Interesting facts about switch statement in C

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;  
   }
   return 0;

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;  
   }
   return 0;

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;

chevron_right


Output:

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

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;
   }
   printf("After Switch");
   return 0;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;
   }
   return 0;
}

chevron_right


Output:

Choice other than 1 and 2

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;                   
   }
   return 0;

chevron_right


Output:

Choice is 1

6) Two case labels cannot have same value

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
                break;
   }
   return 0;

chevron_right


Output:

Compiler Error: duplicate case value

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up

Improved By : Yuvraj Patil