Functions of switch case in C language

Switch case or switch statement can be called as an alternative to if else condition. We use switch statement instead of long if else statement. When we need to do some different operations for different options then we use switch case in C.

Syntax of switch case for C language

See the syntax of switch case for C programming bellow.

switch(a variable){
case 1:
    // code to execute when the value of variable is 1
    break;
case 2:
    // code to execute when the value of variable is 2
    break;
case 3:
    // code to execute when the value of variable is 3
    break;
........................................
}

If we do not use break statement then the program will carry out all the instruction bellow the matched case. For example, you should see run the code bellow;

#include <stdio.h>
int main(){
int x = 2;
switch(x){
case 1:
    printf("Case 1 is matched.\n");
case 2:
    printf("Case 2 is matched.\n");
case 3:
    printf("Case 3 is matched.\n");
default:
    printf("Default case.\n");
}
return 0;
}

When you run this code then you will see all the output bellow the matched case 2 here. The output will be as following.

Case 2 is matched.
Case 3 is matched.
Default case.

Process returned 0 (0x0)   execution time : 0.029 s
Press any key to continue.

But if we use break statement after every case. Then the switch will break after printing the instruction of our matched case only. Let’s try to understand by the correction of this c program which will give the expected output.

#include <stdio.h>
int main(){
int x = 2;
switch(x){
case 1:
    printf("Case 1 is matched.\n");
    break;
case 2:
    printf("Case 2 is matched.\n");
    break;
case 3:
    printf("Case 3 is matched.\n");
    break;
default:
    printf("Default case.\n");
}
return 0;
}

Warning : We can only use integer or character as the value of case. It will give an error if we use other variable as the case value.

Some programs to learn C switch case

We have given a program of vowel or consonant checker by C programming.

#include <stdio.h>
int main(){
char myChar;
printf("Enter the character you want to inspect : ");
scanf("%c", &myChar);

switch(myChar){
case 'a':
    printf("%c is vowel.\n", myChar);
    break;
case 'A':
    printf("%c is vowel.\n", myChar);
    break;
case 'e':
    printf("%c is vowel.\n", myChar);
    break;
case 'E':
    printf("%c is vowel.\n", myChar);
    break;
case 'i':
    printf("%c is vowel.\n", myChar);
    break;
case 'I':
    printf("%c is vowel.\n", myChar);
    break;
case 'o':
    printf("%c is vowel.\n", myChar);
    break;
case 'O':
    printf("%c is vowel.\n", myChar);
    break;
case 'u':
    printf("%c is vowel.\n", myChar);
    break;
case 'U':
    printf("%c is vowel.\n", myChar);
    break;
default:
    printf("%c is consonant.\n", myChar);
    break;
}
return 0;
}

Another program to understand switch case in C. This is the digit spelling program in C.

#include <stdio.h>
int main(){
    int digit;
    printf("Enter your favorite digit : ");
    scanf("%d", &digit);

    switch(digit){
    case 0:
        printf("\n\tZero\n");
        break;

    case 1:
        printf("\n\tOne\n");
        break;

    case 2:
        printf("\n\tTwo\n");
        break;

    case 3:
        printf("\n\tThree\n");
        break;

    case 4:
        printf("\n\tFour\n");
        break;

    case 5:
        printf("\n\tFive\n");
        break;

    case 6:
        printf("\n\tSix\n");
        break;

    case 7:
        printf("\n\tSeven\n");
        break;

    case 8:
        printf("\n\tEight\n");
        break;

    case 9:
        printf("\n\tNine\n");
        break;

    default:
        printf("\n\tNot a digit\n");
    }
return 0;
}

We can also use any valid expression as a case value. If we use 3 + 6 as the case value, then the actual value will be 9 and the compiler will execute the code inside that block if we pass the value 9 inside the switch.

#include <stdio.h>
int main(){
    int favDigit;
    printf("Enter the digit you want to spell : ");
    scanf("%d", &favDigit);

    switch(favDigit){
    case 5 -5:     // 5 - 5 means 0
        printf("\n\tZero\n");
        break;

    case 6 - 5:     // 6 - 5 means 1
        printf("\n\tOne\n");
        break;

    case 2:
        printf("\n\tTwo\n");
        break;

    case 2 + 1:
        printf("\n\tThree\n");
        break;

    case 4:
        printf("\n\tFour\n");
        break;

    case 15 - 10:
        printf("\n\tFive\n");
        break;

    case 6:
        printf("\n\tSix\n");
        break;

    case 35 / 5:
        printf("\n\tSeven\n");
        break;

    case 4 * 2:
        printf("\n\tEight\n");
        break;

    case (5 - 2) * 3:     // (5 -2) * 3 = 9
        printf("\n\tNine\n");
        break;

    default:
        printf("\n\tNot a digit\n");
    }
return 0;
}

Previous pageNested if else in C

Next pageFor loop in C language