Open In App

Interesting facts about switch statement in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisite – Switch Statement in C Switch is a control statement that allows a value to change control of execution. 

C




// Following is a simple program to demonstrate syntax of switch.
#include <stdio.h>
int main()
{
   int x = 2;
   switch (x)
   {
       case 1: printf("Choice is 1");
               break;
       case 2: printf("Choice is 2");
                break;
       case 3: printf("Choice is 3");
               break;
       default: printf("Choice other than 1, 2 and 3");
                
   }
   return 0;
}


Output:

Choice is 2

Following are some interesting facts about switch statement. 1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed. 

C




// float is not allowed in switch
#include <stdio.h>
int main()
{
   float x = 1.1;
   switch (x)
   {
       case 1.1: printf("Choice is 1");
                 break;
       default: printf("Choice other than 1, 2 and 3");
                 
   }
   return 0;
}


Output:

 Compiler Error: switch quantity not an integer

In Java, String is also allowed in switch (See this) 2) All the statements following a matching case execute until a break statement is reached. 

C




// There is no break in all cases
#include <stdio.h>
int main()
{
   int x = 2;
   switch (x)
   {
       case 1: printf("Choice is 1\n");
       case 2: printf("Choice is 2\n");
       case 3: printf("Choice is 3\n");
       default: printf("Choice other than 1, 2 and 3\n");
   }
   return 0;
}


Output:

Choice is 2
Choice is 3
Choice other than 1, 2 and 3

C




// There is no break in some cases
#include <stdio.h>
int main()
{
   int x = 2;
   switch (x)
   {
       case 1: printf("Choice is 1\n");
       case 2: printf("Choice is 2\n");
       case 3: printf("Choice is 3\n");
       case 4: printf("Choice is 4\n");
               break;
       default: printf("Choice other than 1, 2, 3 and 4\n");
                 
   }
   printf("After Switch");
   return 0;
}


Output:

Choice is 2
Choice is 3
Choice is 4
After Switch

3) The default block can be placed anywhere. The position of default doesn’t matter, it is still executed if no match found. 

C




// The default block is placed above other cases.
#include <stdio.h>
int main()
{
   int x = 4;
   switch (x)
   {
       default: printf("Choice other than 1 and 2");
                break;       
       case 1: printf("Choice is 1");
               break;
       case 2: printf("Choice is 2");
                 
   }
   return 0;
}


Output:

Choice other than 1 and 2

4) The integral expressions used in labels must be a constant expressions 

C




// A program with variable expressions in labels
#include <stdio.h>
int main()
{
    int x = 2;
    int arr[] = {1, 2, 3};
    switch (x)
    {
        case arr[0]: printf("Choice 1\n");
        case arr[1]: printf("Choice 2\n");
        case arr[2]: printf("Choice 3\n");
    }
    return 0;
}


Output:

Compiler Error: case label does not reduce to an integer constant

5) The statements written above cases are never executed After the switch statement, the control transfers to the matching case, the statements written before case are not executed. 

C




// Statements before all cases are never executed
#include <stdio.h>
int main()
{
   int x = 1;
   switch (x)
   {
       x = x + 1;  // This statement is not executed
       case 1: printf("Choice is 1");
               break;
       case 2: printf("Choice is 2");
                break;
       default: printf("Choice other than 1 and 2");
                                   
   }
   return 0;
}


Output:

Choice is 1

6) Two case labels cannot have same value 

C




// Program where two case labels have same value
#include <stdio.h>
int main()
{
   int x = 1;
   switch (x)
   {
       case 2: printf("Choice is 1");
               break;
       case 1+1: printf("Choice is 2");
                 
   }
   return 0;
}


Output:

Compiler Error: duplicate case value



Last Updated : 16 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads