Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Last active December 16, 2015 19:38
Show Gist options
  • Select an option

  • Save theonewolf/5486167 to your computer and use it in GitHub Desktop.

Select an option

Save theonewolf/5486167 to your computer and use it in GitHub Desktop.
switch brain teaser
#include <stdio.h>
#include <stdlib.h>
enum option
{
OPT_ONE = 0x01,
OPT_TWO = 0x02,
OPT_THREE = 0x03,
OPT_FOUR = 0x04,
OPT_MAGIC = 0x05
};
void test_switch(enum option opt)
{
switch(opt)
{
OPT_ONE:
fprintf(stdout, "OPT_ONE\n");
break;
OPT_TWO:
fprintf(stdout, "OPT_TWO\n");
break;
OPT_THREE:
fprintf(stdout, "OPT_THREE\n");
break;
OPT_FOUR:
fprintf(stdout, "OPT_FOUR\n");
break;
OPT_MAGIC:
fprintf(stdout, "OPT_MAGIC\n");
break;
default:
fprintf(stdout, "default, no match found.\n");
};
}
int main(int argc, char* argv[])
{
test_switch(OPT_MAGIC);
return EXIT_SUCCESS;
}
@tipt0e
Copy link
Copy Markdown

tipt0e commented Apr 30, 2013

default, no match found.
no case keywords

@peterldowns
Copy link
Copy Markdown

OPT_* are labels, not cases, so the program reaches the default case.

Copy link
Copy Markdown

ghost commented Apr 30, 2013

Perhaps file this against pygments. Emacs at least highlights the difference.

@ZeeD
Copy link
Copy Markdown

ZeeD commented Apr 30, 2013

well... with the right compiler options it doesn't compile.

$ make foo
cc -Werror -Wall -Wextra -Wold-style-definition -Wmissing-declarations -Wredundant-decls -Wmissing-noreturn -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Winline -Wformat-nonliteral -Wformat-security -Wswitch-enum -Wswitch-default -Winit-self -Wmissing-include-dirs -Wundef -Waggregate-return -Wmissing-format-attribute -Wnested-externs -std=c11 -pedantic foo.c -o foo
foo.c: In function ‘test_switch’:
foo.c:16:2: error: enumeration value ‘OPT_ONE’ not handled in switch [-Werror=switch-enum]
switch (opt) {
^
foo.c:16:2: error: enumeration value ‘OPT_TWO’ not handled in switch [-Werror=switch-enum]
foo.c:16:2: error: enumeration value ‘OPT_THREE’ not handled in switch [-Werror=switch-enum]
foo.c:16:2: error: enumeration value ‘OPT_FOUR’ not handled in switch [-Werror=switch-enum]
foo.c:16:2: error: enumeration value ‘OPT_MAGIC’ not handled in switch [-Werror=switch-enum]
foo.c:29:3: error: label ‘OPT_MAGIC’ defined but not used [-Werror=unused-label]
OPT_MAGIC:
^
foo.c:26:3: error: label ‘OPT_FOUR’ defined but not used [-Werror=unused-label]
OPT_FOUR:
^
foo.c:23:3: error: label ‘OPT_THREE’ defined but not used [-Werror=unused-label]
OPT_THREE:
^
foo.c:20:3: error: label ‘OPT_TWO’ defined but not used [-Werror=unused-label]
OPT_TWO:
^
foo.c:17:3: error: label ‘OPT_ONE’ defined but not used [-Werror=unused-label]
OPT_ONE:
^
cc1: all warnings being treated as errors
make: *** [foo] Errore 1
$

@theonewolf
Copy link
Copy Markdown
Author

@ZeeD that's the reason why this was linked to as a "brain teaser" not a "compiler teaser." Although many brains are also good enough for this one it seems :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment