Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler not giving error for alternative name for 'default' case in switch

I found the below code while googling.

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 is not giving an error even if the 'default' is replaced by any other name. It is simply executing the program and exiting the program without printing anything.

Would anyone please tell me why the compiler is not giving an error on default case ? when it is not spelled as 'default'?

like image 821
SatheeshCK17 Avatar asked Jun 14 '13 08:06

SatheeshCK17


2 Answers

It's a normal (goto) label.

You could 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;
}
like image 190
Pubby Avatar answered Sep 19 '22 13:09

Pubby


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

Your statement is a valid statement, it declares a label. If you use -Wall, GCC will warn you about the unused label, not more.

like image 26
Matthieu Rouget Avatar answered Sep 21 '22 13:09

Matthieu Rouget