ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Error: case label does not reduce to an integer constant in C

Here, we will learn why an error case label does not reduce to an integer constant in C language occurred and how to fix it?
Submitted by IncludeHelp, on September 01, 2018

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.

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.

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.

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:

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

C Common Errors Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.