C中的switch语句:变量以防万一? [英] Switch statements in C: variable in case?

查看:42
本文介绍了C中的switch语句:变量以防万一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
int main(int argc, char *argv[]){
    char a = 'c';
    switch('c'){
        case a:
            printf("hi
");
    }
    return 0;
}

上面的代码不会为这个错误编译:

The above won't compile for this error:

case label does not reduce to an integer constant

为什么不允许这样做?

推荐答案

明确允许编译器使用高效的二叉树或跳转表来评估 case 语句.

The compiler is explicitly allowed to use an efficient binary tree or a jump table to evaluate case statements.

因此,case 语句是编译时常量.

For this reason, case statements are compile time constants.

这篇关于C中的switch语句:变量以防万一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆