c - 宏作为 switch-case 中的一个案例在 c 中不起作用

标签 c macros switch-statement

我正在做一个大学项目。我在这里遇到的问题是这个问题的过度简化版本..

#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/

相关文章:

linux - 如何让switch执行2个case

c - pthread_create创建的线程是否与内核线程相同?

c - 如何在c中比较两个无符号整数(uint8_t)

c++ - 查找硬件中正在使用哪个中断驱动程序的命令是什么?

csv - ImageJ宏: saving csv file

java - Switch 语句未初始化的变量

java - 如果 jobobject 从 JNI 返回到 JAVA,我应该删除 GlobalRef 吗?

excel - 如何 VBA 选择一对工作表并将它们复制到新的 wb?

c++ - 代码无法选择声明的宏

typescript - noFallthroughCasesInSwitch - 明确允许失败