What does comma operator mean in a switch statement?

后端 未结 6 489
-上瘾入骨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条回答
  •  猫巷女王i
    2021-01-17 08:18

    Turbo C uses comma operator on switch cases and takes the last value, for example case 1, 2, 3: will be compiled as case 3: case 2, 3, 1 as case 1: hence Turbo C will not give you any error. Where as other compilers will not allow you case 1, 2, 3: kind of statement itself.

    But in your case even Turbo c will give error because case statements are something like this case 1, 2, 1: and case 3, 2, 1: which will be complied as case 1: and case 1: hence as per switch case rules you can have only 1 case with a value and you cannot repeat the case

    I prefer to use gcc compiler rather Turbo C

提交回复
热议问题