Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Created September 4, 2015 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aperezdc/0756bc8cd8ec59544c19 to your computer and use it in GitHub Desktop.
Save aperezdc/0756bc8cd8ec59544c19 to your computer and use it in GitHub Desktop.
Jump to label inside a switch statement in C
/*
* 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