C 标签不会减少为整数常量
运行此c程序时出错。我得到了一个错误,比如“case label没有减少到整数常量”。帮助我发现错误。我是c的新手,几周前开始的。提前谢谢C 标签不会减少为整数常量,c,switch-statement,C,Switch Statement,运行此c程序时出错。我得到了一个错误,比如“case label没有减少到整数常量”。帮助我发现错误。我是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 ("state
#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;
}
}
#包括
main()
{int a,b,c;
scanf(“%d”、&c);
如果(c在c中,您似乎出于某种原因调用了c
,case
标签必须是整数常量
6.8.4.2-3
每个箱子标签的表达式应为整数常量
表达式,并且没有两个大小写常量表达式在同一个
转换后的开关语句应具有相同的值
不确定这是否是您想要的,但您可以尝试:
switch (c) {
case 'a':
break;
case 'b':
break;
}
否则,也许你想要
if (c == a)
/* ... */
else if (c == b)
/* ... */
else
/* ... */
作为旁注,您可能需要c=a
而不是c==a
2个问题:
- 案例标签的值需要解析(即结束)作为<代码>案例<代码>语句的整数值。如果您想根据<代码> C <代码>的值来驱动<代码>案例<代码>中的语句,并将其与<代码> < <代码> >代码> b>代码>进行比较,您可能需要考虑<<代码>如果>语句。
- 您需要在此处执行赋值
=
,而不是布尔比较=
c==a
可能想要
c = a
(同样适用于c==b
)-
事实上,您确定这是正确的顺序,并且您不希望使用相反的a=c
?这看起来更可能是针对您的代码段
您是将整数c赋值给a还是b?您应该使用=not==并将a或b放在赋值的左侧,而不是右侧。如下所示:
if (c<5) {
a=c;
}
如果(c我更正了你的代码。请尝试并给我反馈:
#include <stdio.h>
using namespace std;
int main()
{
int a,b,c;
scanf("%d",&c);
if (c<5)
{
a = c; //here you will use "=" because you want to a became c
}
else
{
b = c; //here you will use "=" because you want to b became c
}
/*--------IF-------------*/
if(c==a) //here is a condition. you will use "=="
{
printf("statement1");
}
if(c==b) //here is a condition. you will use "=="
{
printf ("statement2");
}
/*--------SWITCH-------------*/
switch (c)
{
case 1: //if c is 1
case 2: //if c is 2
case 3: //if c is 3
case 4: //if c is 4
printf("statement1"); //"statement1" will appear
break;
default: printf("statement2"); //if c >= 5 "statement2" will appear
}
return 0;
}
#包括
使用名称空间std;
int main()
{
INTA、b、c;
scanf(“%d”、&c);
如果(c=5),将出现“语句2”
}
返回0;
}
你想用c==a;
和c==b;
语句实现什么?看看代码,我想他实际上想把c与变量a和b进行比较,所以他应该做的是用if/else替换switch/case。谢谢你,约翰。你的代码运行得很好。我能用“switch”编写同样的程序吗。这就是我打算做的。因此,我看到你标记了另一个答案,但你可以再次标记我的答案。无论如何,你在等待什么答复?你对我的代码做了哪些更改,我很想知道。无法理解我编辑了答案。请参阅注释。谢谢你的标记。:)我不明白您为什么要使用开关…无论如何,请参见编辑。例如,您有两个变量a=5和b=7。您将使用“=”将a变成b:a=b。a将是7。如果您写入b=a,b将是5。您将在If(a==b)、while(a==b)等条件下使用“==”。希望您理解……)hii levon。我这样更正了我的程序:将==更改为=,a=c而不是c=a,与b相同。但是得到了相同的错误。我的目的是得到一个输出,就像@john使用switch@user1557910这是不可能的。在开关
语句中,所有大小写
标签必须是编译时已知的整数常量时间
#include <stdio.h>
using namespace std;
int main()
{
int a,b,c;
scanf("%d",&c);
if (c<5)
{
a = c; //here you will use "=" because you want to a became c
}
else
{
b = c; //here you will use "=" because you want to b became c
}
/*--------IF-------------*/
if(c==a) //here is a condition. you will use "=="
{
printf("statement1");
}
if(c==b) //here is a condition. you will use "=="
{
printf ("statement2");
}
/*--------SWITCH-------------*/
switch (c)
{
case 1: //if c is 1
case 2: //if c is 2
case 3: //if c is 3
case 4: //if c is 4
printf("statement1"); //"statement1" will appear
break;
default: printf("statement2"); //if c >= 5 "statement2" will appear
}
return 0;
}