Created
September 4, 2015 11:13
-
-
Save aperezdc/0756bc8cd8ec59544c19 to your computer and use it in GitHub Desktop.
Jump to label inside a switch statement in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Silly example demonstrating that it is valid to jump | |
* to arbitrary labels inside "switch" statements using | |
* "goto" in C. | |
*/ | |
bool is_not_small_power_of_two (int value) | |
{ | |
if (value < 0) { | |
/* It is legal to jump into a switch */ | |
goto do_return_false; | |
} | |
switch (value) { | |
case 2: | |
case 4: | |
case 8: | |
case 16: | |
case 32: | |
case 64: | |
case 128: | |
default: | |
do_return_false: /* Label inside "switch" */ | |
return false; | |
} | |
return true; | |
} | |
int | |
main (int arcg, char *argv[]) | |
{ | |
if (argc != 2) { | |
fprintf (stderr, "Usage: %s number\n", argv[0]); | |
return 111; | |
} | |
return is_not_small_power_of_two (atoi (argv[1])) | |
? EXIT_SUCCESS | |
: EXIT_FAILURE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment