逗号运算符在switch语句中的含义是什么?

Tus*_*waj 27 c c++

我收到了一个问题,并被要求提供输出.

int main(void){  
    int x = 2;  
    switch(x){  
        case 1,2,1: printf("Case 1 is executed");  
            break;  
        case 2,3,1: printf("Case 2 is executed");  
            break;  
        default : printf("Default case us executed");  
    }  
    return 0;  
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在Turbo C中给出了"Case 1 is execution"的输出,但是在代码块和在线编译时,它给出了编译器错误.

哪一个是正确的?这是编译器错误吗?如果没有,为什么代码只在Turbo C上运行?

Mik*_*our 38

它是否是编译器错误.

两种语言的代码都是无效的:case表达式必须是常量表达式,而常量表达式不能包含逗号运算符.(在C中,这是明确说明的;在C++中,您必须取消语法以发现常量表达式必须是条件表达式,它不能包含逗号).

即使您在此处被允许使用逗号运算符,该switch语句仍然无效,因为两个案例都具有相同的值1.

如果没有,为什么代码只在turbo C上运行.

因为自史前编译器上次更新以来,这两种语言都发生了重大变化.如果你想从本世纪学习C或C++的变体,请不要使用它.

  • 同意@Mike - 如果您使用Code :: Blocks,minGW是一个维护良好的开源编译器,免费提供.这将是比Turbo C更好的选择. (8认同)
  • 本世纪唯一的C形式是C11.并非所有"现代"编译器都支持C99. (7认同)

ryy*_*ker 10

逗号运算符在switch语句中的含义是什么?
这意味着你有一个旧的编译器.

编辑帖子 (显示case range示例)

前两个示例(包括您的原始代码)显示错误的switch语句语法(带有解释).第三个代码示例显示堆栈案例标签是如何正确完成的:

在您的代码中,编译器应该在case 1,< - here 之后标记第一个逗号

#include <ansi_c.h>
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;  
}  
Run Code Online (Sandbox Code Playgroud)

而且,即使像这样修改,您也应该得到重复的标签错误:

#include <ansi_c.h>
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;  
}
Run Code Online (Sandbox Code Playgroud)

这个例子是完全合法的(C99,C11)和有用的:即,没有重复的标签,并且语法符合正确的开关使用,通过堆叠唯一标签来处理case 1: OR case 2: OR case 3:应该以相同方式处理的条件,(在同一块中) .当然,对于案例4,5和6也是如此.

#include <ansi_c.h>
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;  
}
Run Code Online (Sandbox Code Playgroud)

最后一个示例仅用于完整性.它说明了case range表达方式.虽然在C程序员中引起了人们的兴趣,但它还不是C99或C11的一部分,而是 Sun(unix的一种风格) GNU C编译器 (等)的扩展. :

...
    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");  
    }
...
Run Code Online (Sandbox Code Playgroud)

从一个编译器到另一个编译器看到的模糊结果的原因可能是Turbo C真的老了.您使用的版本可能是针对不再是最新的C标准版本实施的.

考虑更改为当前编译器.MinGW是一种廉价(免费)的替代品.MinGW是一个维护良好的开源编译器.如果您喜欢使用集成开发环境(IDE), Code :: Blocks是一个选项,也是免费的,并且作为选项与MinGW捆绑在一起.

关于兼容性,请在此链接中搜索与其他编译器套件的比较以阅读有关MinGW扩展的信息.MinGW扩展虽然扩展了功能,但有时会使用它们编写的代码与其他当前编译器不兼容.建议在使用时小心谨慎.