gpt4 book ai didi

c - 标签不会减少为整数常量

转载 作者:行者123 更新时间:2023-12-02 06:05:13 24 4
gpt4 key购买 nike

运行此 C 程序时出错。我收到类似“case label doesnot reduce to an integer constant”的错误。帮我找出我的错误。我是 c 的新手,几周前开始使用它。提前致谢

 #include<stdio.h>
main()
{ int a,b,c;
scanf("%d",&c);
if (c<5) {
c==a ;
}
else { c==b;
}
switch (c)
{
case a:
printf ("statement1");
break;
case b :
printf(" statement2");
break;
}
}

最佳答案

在 C 语言中,您似乎出于某种原因调用了 c#case 标签必须是整数常量

6.8.4.2-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.

不确定这是否是您想要的,但您可以尝试:

switch (c) {
case 'a':
break;
case 'b':
break;
}

否则,也许你想要

if (c == a)
/* ... */
else if (c == b)
/* ... */
else
/* ... */

作为旁注,您可能需要 c=a 而不是 c==a

关于c - 标签不会减少为整数常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11689912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com