I came up with this program in some other site and thought of trying it, here's the program:
#include <stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1': printf("one");
break;
case '2': printf("two");
break;
defau4t: printf("none");
}
return 0;
}
Suprisingly enough, this compiles without errors or warnings. How is this possible? Isn't there an error on keyword "default"?
Could anyone explain this behaviour?
The token is not considered to be a keyword at all. This is a goto
jump mark named "defau4t" pointing at otherwise dead code (after the break;
of case '2':
)...
Try this for laughs (and an endless loop):
switch(a)
{
case '1': printf("one");
break;
case '2': printf("two");
break;
defau4t: printf("none");
default: goto defau4t;
}
One flaw with the switch
statement is that you can wildly jump in and out of them using goto
. At any point inside the switch
(or outside it for that matter), you can place a label, that you can jump to with goto
. Of course, that is very bad practice as it leads to spaghetti code.
So defau4t:
is merely a label, and labels can be placed pretty much anywhere inside function bodies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With