我正在做一个大学项目。我在这里遇到的问题是这个问题的过度简化版本..
#include <stdio.h>
#define h1 2
#define h2 3
#define h3 4
const int ar[] = {h1,h2,h3};
int main(int argc, char const *argv[]) {
for (size_t i = 0; i < 3; i++) {
switch (i) {
case ar[i]:
printf("yes!!!!!!!!");
}
}
return 0;
}
显示错误。看不懂,请解释一下... 谢谢。
最佳答案
在C语言中,case标签必须是编译时常量。在您的代码中,ar[i] 不是编译时间常数。
C11 6.8.4.2 switch 语句(第 3 段):
The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion.
关于c - 宏作为 switch-case 中的一个案例在 c 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50188328/