Semicolon at the end of switch : C Programming



In this tutorial we will look into the special case of the switch case statement where we have semicolon at the end of switch case statement.

Semicolon at the end of switch :

void main()
{
 int choice = 2 ;
 switch(choice);
 {
  case 1 :
        printf("Case 1");
        break;

  case 2 :
        printf("Case 2");
        break;

  case 3 :
        printf("Case 3");
        break;
 }
 printf(" Finally I am in main ");
}

Output :

Finally I am in main

Explanation

In this example we have written a switch case and there is semicolon at the end of switch statement. In C programming all the statements ends with the semicolon.

switch(choice);

In the above statement once we write semicolon, complete block of the switch will be ignored by the compiler. It will create bodyless switch case statement like –

switch(choice) {
}

In this example the value of variable choice is equal to 2. Though value of choice is 2 we dont have anything to execute because above example can be interpreted as –

Semicolon at the end of switch