Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"defau1t:" (NOT "default:") is a valid value for label? [duplicate]

Tags:

c++

c

Why this code compile and run fine. I have written defau1t instead of default , 1 at the place of l.

#include<stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        case 3:
        break;
        defau1t :
        break;      
    }
}
like image 391
Nikesh Joshi Avatar asked Jun 08 '16 12:06

Nikesh Joshi


2 Answers

defau1t : is a valid label name, even if it is not a case label.

You could have a goto defau1t; somewhere else in the code.

like image 139
Bo Persson Avatar answered Oct 19 '22 16:10

Bo Persson


It's a valid name for a label, which you can use as a placeholder for a goto call. (And entering into a switch block via a goto call is allowable in C and C++ even if it's ill-advised).

label names broadly have the same rules as variable names when it comes to the characters they can contain. defau1t satisfies those rules.

In your case, it's benign and will be compiled out at runtime, although a good compiler will warn you that it's not used.

like image 28
Bathsheba Avatar answered Oct 19 '22 16:10

Bathsheba