Q:

Error: Executing more than one case block in C

belongs to collection: C Common Errors Programs

0

Error: Executing more than one case block in C

Executing more than one case block in C – is not a syntax error, it's a logical error and it occurs when you miss to place break statement in the case block.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

#include <stdio.h>

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

Output

Case 2
Case 3
Case 4
Case default

How to fix?

Use break with each case block.

Correct code:

#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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Error: switch quantity not an integer in C... >>
<< Error: duplicate case value in C...