Here is a simple example (not tied to specific platform):
#include <stdio.h>
int main()
{
for(int i = 0; i < 3; ++i)
if(i == 0) printf("1\n");
else if (i == 1) goto checkfor2;
else checkfor2: if(i == 2 || i == 1) printf("2\n");
}
Live wandbox.
My original Windows code from which this question raised:
#include <Windows.h>
#include <stdbool.h>
main() //sample with Win32 API
{
for (;;) //infinite loop for my main logic
if (WaitForSingleObject(hEvent, 1) != WAIT_TIMEOUT, true) //#1 check for some event
//oops set comma true to test this event code
{
//do something
//but here it will loop infinte and wouldn't check for #3
//so if something goes wrong I can't exit
//so clever me did the following:
goto checkforexit; //see label on #3 below
}
else if (WaitForSingleObject(hEvent2, 1) != WAIT_TIMEOUT) //#2 check for another event
{
//do something else
}
else checkforexit: /*wow in between else and if*/ if (GetAsyncKeyState(VK_BACK) & 0x8000 && GetAsyncKeyState(VK_CONTROL) & 0x8000) //#3 check for keyboard shortcut for exit
exit(0); //termination code
}
I was surprised that this worked - it seems both the original else if and the label to the if are working as intended - how is this possible?
I would appreciate if you add a quote to the C standard.
It's working both on gcc (as tested on wandbox) and MSVC.