What does comma operator mean in a switch statement?

后端 未结 6 409
-上瘾入骨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:04

    What does comma operator mean in a switch statement?
    It means you have an old compiler.

    Edit post (to show case range example)

    The first two examples (including your original code ) exhibit incorrect switch statement syntax (with explanations). The third code example shows how stacking case labels is done correctly:

    In your code, the compiler should have flagged the first comma after case 1,<-- here

    #include 
    int main(void){  
        int x = 2;  
        switch(x)
        {  
            case 1,2,1: printf("Case 1 is executed");  
            break;  //error flagged at first comma, and all comma after in case
            case 2,3,1: printf("Case 2 is executed");  
            break;  
            default : printf("Default case is executed");  
        }  
        return 0;  
    }  
    

    And, even modified like this you should also get a duplicate label error:

    #include 
    int main(void){  
        int x = 2;  
        switch(x)
        {  
            case 1:
            case 2:
            case 1: printf("Case 1 is executed"); //duplicate label 1 error. (and others below) 
                break;  
            case 2:
            case 3:
            case 1: printf("Case 2 is executed");  
                break;
    
            default : printf("Default case is executed");  
        }
        return 0;  
    }
    

    This example is perfectly legal (C99, C11) and useful: i.e., there are no duplicate labels, and the syntax complies with correct switch usage by stacking unique labels to handle conditions where case 1: OR case 2: OR case 3: should be handled the same way, (in the same block). And of course the same is true for cases 4, 5 and 6.

    #include 
    int main(void){  
        int x = 2;  
        switch(x)
        {  
            case 1:
            case 2:
            case 3: printf("Case 1,2 or 3 is executed"); //duplicate label 1 error. (and others below) 
                break;  
            case 4:
            case 5:
            case 6: printf("Case 4,5 or 6 is executed");  
                break;
        }
        getchar();
        return 0;  
    }
    

    This last example is included just for completeness. It illustrates the case range expression. Although gaining interest among C programmers, it is not yet part of C99 or C11, rather an extension of Sun (a flavor of unix) and GNU C compiler (et al):

    ...
        switch(x)
        {  
                case 'a' ... 'z':  //note: spaces between all characters ('a') and ellipses are required
                        printf("lowercase alpha char detected");
                        break;
                case 'A' ... 'B':
                        printf("uppercase alpha char detected");
                        break;
    
                default: printf("Default case is executed");  
        }
    ...
    

    The reason for the ambiguous results you are seeing from one compiler to another may be that Turbo C is really really old. The version you are using was likely implemented against a version of the C standards that is no longer current.

    Consider changing to a current compiler. An inexpensive (free) alternative is MinGW. MinGW is a very well maintained, open source compiler. If you like using Integrated Development Environments (IDE), Code::Blocks is one option, also free, and as an option comes bundled with MinGW.

    Regarding compatibility, search for Comparison to Other Compiler Suites in this link to read about MinGW extensions. MinGW extensions, while expanding capabilities, sometimes make code written using them non-portable with other current compilers. Recommend using caution when using them.

提交回复
热议问题