警告:整数常量值超出其类型_错误:案例标签未减少为C中的整数常量

警告:整数常量值超出其类型

This is an example of switch case in C programming language, switch case is used to check/jump on matched case value and then it executes given statement in the case block.

这是用C编程语言编写的switch case的示例,switch case用于检查/跳转匹配的case值,然后在case块中执行给定的语句。

In the switch case statement, a case can only have integral constant values i.e. integer or character type constant value. We cannot use any variable as case value.

在switch case语句中, case只能具有整数常量值,即整数或字符类型常量值。 我们不能使用任何变量作为case值

In this example, we are using case b: and b is a variable. Thus, error case label does not reduce to an integer constant occurs.

在此示例中,我们使用情况b:并且b是变量。 因此, 错误案例标签不会减少到整数常量发生

Example:

例:

#include <stdio.h>

int main()
{
    int a,b;
    
    a=2;
    b=2;
    
    switch(a){
        case 1:
            printf("Case 1\n");
            break;
        
        case b:
            printf("Case 2\n");
            break;
    }
    
	return 0;
}

Output

输出量

prog.c: In function ‘main’:
prog.c:15:9: error: case label does not reduce to an integer constant
         case b:
         ^~~~
prog.c:5:11: warning: variable ‘b’ set but not used [-Wunused-but-set-variable]
     int a,b;
           ^

How to fix?

怎么修?

To fix the error case label does not reduce to an integer constant, use value 2 instead of variable b. The correct statement will be case 2:

修复错误情况标签不会减少为整数常量的情况 ,请使用值2而不是变量b 。 正确的陈述将是情况2:

Correct code:

正确的代码:

#include <stdio.h>

int main()
{
    int a,b;
    
    a=2;
    b=2;
    
    switch(a){
        case 1:
            printf("Case 1\n");
            break;
        
        case 2:
            printf("Case 2\n");
            break;
    }
    
	return 0;
}

Output

输出量

Case 2


翻译自: https://www.includehelp.com/c-programs/case-label-does-not-reduce-to-an-integer-constant-error-in-c.aspx

警告:整数常量值超出其类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值