【发布时间】:2013-10-02 14:44:07
【问题描述】:
我编写此代码仅用于学习目的。但是在这样做的时候我发现了一个问题。这里 x 是常量整数,编译器仍然给我错误。我正在使用 gcc 编译器。请解释这个错误的原因以及如何避免它。
#include<stdio.h>
int main()
{
int const x = 10;
int y = 20;
switch(y)
{
case x: //error: case label does not reduce to an integer constant
printf("value of x: %d\n",x);
break;
}
}
【问题讨论】:
-
如果它是一个常数,为什么不使用
case 10:,你还需要defaultcase -
@user1 例如为了可读性。记住幻数不好。
标签: c