Monday, 30 June 2014

Yet Another Difference Between C and C++: Case Labels

 There are many major differences between C and C++. Such differences are matters for Text Books and Manuals. Here I am pointing out yet another minor difference between C and C++. This time the difference arises from the way case labels are treated in C and C++. 

 C++ will allow const int variables as case labels, whereas C will give you an error. Consider the program given below.


int main()
    int i=1;
    const int j=1;
        case j:
        case 2:
    return 0;

The program will give the error 'case label does not reduce to an integer constant' when compiled as a C program. But when compiled as a C++ program the program executes without any errors and gives 'Hello' as output. 

No comments:

Post a Comment