What does comma operator mean in a switch statement?

后端 未结 6 418
-上瘾入骨i
-上瘾入骨i 2021-01-17 07:31

I was given a question and was asked to give the output.

int main(void){  
    int x = 2;  
    switch(x){  
        case 1,2,1: printf(\"Case 1 is executed\         


        
6条回答
  •  无人及你
    2021-01-17 08:01

    is it a compiler error or not.

    It will cause a compilation error (don't know about TURBO C++ but in modern compilers).
    This is not the way switch statement works (invalid syntax in c/c++). You cannot reuse a case value in switch statement (see the link given by chris) The way you can do this;

     switch(x){
                case 1: case 2: case 3: printf("Case 1 is executed");
                break;
                default : printf("Default case us execyted"); 
              }
    

提交回复
热议问题