C语言 切换案例:错误:案例标签不会减少为整数常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14069737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
switch case: error: case label does not reduce to an integer constant
提问by Jim Clermonts
int value;
const int signalmin = some_function();
switch(value)
{
case signalmin:
break;
}
I read the value of some_function and use that int value to do a switch case on. The C99 compiler gives back:
我读取了 some_function 的值并使用该 int 值来执行 switch case。C99 编译器返回:
error: case label does not reduce to an integer constant
错误:案例标签不会减少为整数常量
But I cannot use a #definebecause the int value is being read before the switch executes.
但是我不能使用 a#define因为在 switch 执行之前正在读取 int 值。
回答by Daniel Fischer
switchlabels must be constant expressions, they have to be evaluated at compile time. If you want to branch on run-time values, you must use an if.
switch标签必须是常量表达式,它们必须在编译时计算。如果要对运行时值进行分支,则必须使用if.
A const-qualified variable is not a constant expression, it is merely a value you are not allowed to modify.
甲const-qualified变量不是常量表达式,它仅仅是你不允许修改的值。
The form of integer constant expressions is detailed in 6.6 (6) [C99 and the n1570 draft of the C2011 standard]:
整数常量表达式的形式详见6.6(6)【C99和C2011标准的n1570草案】:
6 An integer constant expressionshall have integer type and shall only have operands that are integer constants, enumeration constants, character constants,
sizeofexpressions whose results are integer constants,_Alignofexpressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to thesizeofor_Alignofoperator.
6整数常量表达式应具有整数类型,并且只能具有整数常量、枚举常量、字符常量、
sizeof结果为整数常量的_Alignof表达式、表达式和作为强制转换的直接操作数的浮点常量的操作数。整数常量表达式中的强制转换运算符只能将算术类型转换为整数类型,除非作为sizeofor_Alignof运算符的操作数的一部分。
The restriction that only sizeofexpressions whose result is an integer constant are allowed rules out sizeofexpressions whose operand is a variable length array.
仅sizeof允许结果为整数常量的表达式的限制排除了sizeof操作数为可变长度数组的表达式。
回答by rath
Let me chip in with an example. The following was tested on gcc version 4.6.3with the flags -std=c99 -pedanticset:
让我用一个例子来说明。以下是在设置4.6.3了标志的gcc 版本上测试的-std=c99 -pedantic:
#define SOME_HARDCODED_CONSTANT 0 //good
int foo(int i, int b){
const int c=0; //bad
int a=0; //bad
switch(i){
case c: //compile error
case a: //compile error.
case (b+a): //compile error
case SOME_HARDCODED_CONSTANT: //all good
case 5: //all good
}
}
As others have noted, casearguments cannot be evaluated at runtime. Use an if-elseblock to do that.
正如其他人所指出的,case无法在运行时评估参数。使用if-else块来做到这一点。
回答by John Bode
In C. all caselabels must be compile timeconstants. In C, the constqualifier does not create a compile-time constant, it merely designates that a run-time variable is read-only.
在 C 中,所有case标签都必须是编译时常量。在 C 中,const限定符不创建编译时常量,它只是指定运行时变量是只读的。
A switchis not the appropriate control structure for what you're trying to do.
Aswitch不是您要执行的操作的适当控制结构。
回答by MajorBetaVictory
In C, variables aren't to be used in the switch case labels instead constant expressions are only allowed there.
在 C 中,不能在 switch case 标签中使用变量,而只允许在那里使用常量表达式。
回答by Farley
On OSX, clang seems to take constants as case labels without complaints.
在 OSX 上,clang 似乎将常量作为案例标签而没有抱怨。
#include <stdio.h>
#define SOME_HARDCODED_CONSTANT 0 //good for sure
int foo(int i, int b){
const int c=1; //no problem!!!
switch(i){
case SOME_HARDCODED_CONSTANT: //all good
printf("case SOME_HARDCODED_CONSTANT\n"); break;
case c: //no compile error for clang
printf("case c\n"); break;
case 5: //all good
printf("case 5\n"); break;
}
return i+b;
}
int main() {
printf("test foo(1,3): %d\n", foo(1,3));
}
Output:
输出:
$> cc test.c -o test; ./test
case c
test foo(1,3): 4

