ADVERTISEMENT
ADVERTISEMENT

Error: duplicate case value in C

Here, we will learn why an error: duplicate case value is occurred in C programming language and how to fix it?
Submitted by IncludeHelp, on September 02, 2018

The error: duplicate case value occurs in C programming, if there are two duplicate case values in the switch statement.

Consider the below program – In this program, there are two case values, which are same. "Case 2" exists two times in the program.

#include <stdio.h>

int main(void) {
	
	int choice = 2;
	switch(choice){
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;	    
	    case 3:
	        printf("Case 3\n");
	        break;	    
	    case 4:
	        printf("Case 4\n");
	        break;	    
	    case 2:
	        printf("Case 5\n");
	        break;	    
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

prog.c: In function ‘main’:
prog.c:19:6: error: duplicate case value
      case 2:
      ^~~~
prog.c:10:6: error: previously used here
      case 2:
      ^~~~
ADVERTISEMENT

How to fix?

To fix the error: duplicate case value in C language, either remove the duplicate case and its block or change the duplicate case value.

Correct code – Here, I am removing the duplicate case "Case 2" which is exist second time in the program.

#include <stdio.h>

int main(void) {
	
	int choice = 2;
	switch(choice){
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;	    
	    case 3:
	        printf("Case 3\n");
	        break;	    
	    case 4:
	        printf("Case 4\n");
	        break;	    
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

Case 2

C Common Errors Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

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.