SatheeshCK17
SatheeshCK17

Reputation: 523

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'?

Upvotes: 6

Views: 160

Answers (2)

Pubby
Pubby

Reputation: 53017

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;
}

Upvotes: 6

Matthieu Rouget
Matthieu Rouget

Reputation: 3369

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.

Upvotes: 2

Related Questions