Switch Case In C

In this easy C language tutorial, we will learn everything about switch case statement in C programming, how to use and where to use a switch case.
Some times, a variable can have more than one values, and each value needs to execute different statements. For example, month is a variable, and the month has 12 different values, you need to execute different statement for each month. So, we can say switch statement is used solve multiple option types problem.

Syntax:
switch(expression)
{
	case value_1:
		//statements
		break;
	case value_2:
		//statements
		break;
	case value_3:
		//statements
		break;
	default:
		//statements
}

Example 1: switch case example in c, to print week day.

#include<stdio.h>
int main()
{
int day;
	printf("\nEnter Weeknum");
	scanf("%d", &day);
	switch(day)
	{
	case 1:
		printf("\nSunday");
		break;
	case 2:
		printf("\nMonday");
		break;
	case 3:
		printf("\nTuesday");
		break;
	case 4:
		printf("\nWednesday");
		break;
	case 5:
		printf("\nThursday");
		break;
	case 6:
		printf("\nFriday");
		break;
	case 7:
		printf("\nSaturday");
		break;
	default:
		printf("\nWrong Choice");
	}
return 0;
}
Sample Output:
Enter Weeknum : 3
Tuesday

Rules for Switch Case Statement in C

  • the switch statement allows a variable to be tested, with the list of values, called the case.
  • the switch statement can have any number of case.
  • All case must end with a colon ( : ).
  • switch, only accept int or char variable as a parameter.
  • float and text values are not allowed.
  • If, no case value is matched than default is executed.
  • the default is optional, you can skip in your program.
  • the break is to end processing and exit from the switch.

Example 2: C program, to accept an alphabet, and check it is vowel or consonant.

#include<stdio.h>
int main()
{
char alph;
	printf("\nEnter an Alphabet");
	scanf("%c", &alph);
	switch(alph)
	{
	case 'a':
	case 'A':
	case 'e':
	case 'E':
	case 'i':
	case 'I':
	case 'o':
	case 'O':
	case 'u':
	case 'U':
		printf("\nVowel");
		break;
	default:
		printf("\nConsonant");
	}
return 0;
}
Sample Output:
Enter an Alphabet : A
Vowel

Mini Quiz

0%

C Programming - Quiz 7

1 / 5

Q1. What is the primary purpose of the switch statement in C?

2 / 5

Q2. Which keyword is used to exit a switch case?

3 / 5

Q3. Which data types can be used as the expression in a switch statement?

4 / 5

Q4. Which block is executed if no case value matches the expression?

5 / 5

Q5. What symbol must appear after every case value in a switch statement?

Your score is

The average score is 80%

0%

Mini Project

Mini Project: Simple Menu-Driven Calculator

Question:

Write a C program that displays the following menu and accepts the user’s choice.

  • Addition
  • Subtraction
  • Multiplication
  • Division

Then, accept two numbers from the user and perform the selected operation using the switch statement. If the user enters an invalid choice, display “Invalid Choice”.

View Answer Code
#include<stdio.h>

int main()
{
    int choice, num1, num2;

    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");

    printf("\nEnter Your Choice: ");
    scanf("%d", &choice);

    printf("Enter Two Numbers: ");
    scanf("%d%d", &num1, &num2);

    switch(choice)
    {
        case 1:
            printf("Result = %d", num1 + num2);
            break;

        case 2:
            printf("Result = %d", num1 - num2);
            break;

        case 3:
            printf("Result = %d", num1 * num2);
            break;

        case 4:
            printf("Result = %d", num1 / num2);
            break;

        default:
            printf("Invalid Choice");
    }

    return 0;
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top