The compiler does not give an error for the alternate name for the "default" case in the switch

I found the code below when searching on Google.

int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } return 0; } 

The compiler does not give an error, even if the "default" is replaced by any other name. It is simply running the program and exiting the program without printing. A.

Will someone tell me why the compiler does not give a default error? when it is not written as "default"?

+6
source share
2 answers

This is a normal label (goto).

You can do this, for example:

 int main() { int a=10; switch(a) { case '1': printf("ONE\n"); break; case '2': printf("TWO\n"); break; defa1ut: printf("NONE\n"); } goto defa1ut; return 0; } 
+6
source

If you use GCC, add -Wall to the options.

Your expression is a valid expression, it declares a label. If you use -Wall, GCC will warn you of an unused label, no more.

+2
source

Source: https://habr.com/ru/post/947281/


All Articles