ADVERTISEMENT
ADVERTISEMENT

Error: case label not within a switch statement in C

Here, we will learn why an error: case label not within a switch statement occurs and how to fix it in C programming language?
Submitted by IncludeHelp, on September 04, 2018

The error: case label not within a switch statement occurs in C language with switch case statement, when switch (variable/value) statement is terminated by the semicolon (;).

Example:

#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

prog.c: In function ‘main’:
prog.c:9:6: error: case label not within a switch statement
      case 1:
      ^~~~
prog.c:11:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:12:6: error: case label not within a switch statement
      case 2:
      ^~~~
prog.c:14:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:15:6: error: case label not within a switch statement
      case 3:
      ^~~~
prog.c:17:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:18:6: error: case label not within a switch statement
      case 4:
      ^~~~
prog.c:20:10: error: break statement not within loop or switch
          break;
          ^~~~~
prog.c:21:6: error: ‘default’ label not within a switch statement
      default:
      ^~~~~~~

How to fix?

See the statement switch(choice); it is terminated by semicolon (;) – it must not be terminated. To fix this error, remove semicolon after this statement.

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

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.