BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – switch case statement in C Programming with example

Last Updated: October 7, 2019 by Chaitanya Singh | Filed Under: c-programming

The switch case statement is used when we have multiple options and we need to perform a different task for each option.

C – Switch Case Statement

Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.

switch (variable or an integer expression)
{
     case constant:
     //C Statements
     ;
     case constant:
     //C Statements
     ;
     default:
     //C Statements
     ;
}

Flow Diagram of Switch Case

C switch case

Example of Switch Case in C

Let’s take a simple example to understand the working of a switch case statement in C program.

#include <stdio.h>
int main()
{
     int num=2;
     switch(num+2)
     {
         case 1:
           printf("Case1: Value is: %d", num);
         case 2:
           printf("Case1: Value is: %d", num);
         case 3:
           printf("Case1: Value is: %d", num);
         default:
           printf("Default: Value is: %d", num);
    }
    return 0;
}

Output:

Default: value is: 2

Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where num value is 2 and after addition the expression resulted 4. Since there is no case defined with value 4 the default case is executed.

Twist in a story – Introducing Break statement

Before we discuss more about break statement, guess the output of this C program.

#include <stdio.h>
int main()
{
     int i=2;
     switch (i)
     {
        case 1:
           printf("Case1 ");
        case 2:
           printf("Case2 ");
        case 3:
           printf("Case3 ");
        case 4:
           printf("Case4 ");
        default:
           printf("Default ");
     }
    return 0;
}

Output:

Case2 Case3 Case4 Default

I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2, However there are no such statements in the above program which could break the flow after the execution of case 2. That’s the reason after case 2, all the subsequent cases and default statements got executed.

How to avoid this situation?
We can use break statement to break the flow of control after every case block.

Break statement in Switch Case

Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.

Example of Switch Case with break
I’m taking the same above that we have seen above but this time we are using break.

#include <stdio.h>
int main()
{
     int i=2;
     switch (i)
     {
          case 1:
             printf("Case1 ");
             break;
          case 2:
             printf("Case2 ");
             break;
          case 3:
             printf("Case3 ");
             break;
          case 4:
             printf("Case4 ");
             break;
          default:
             printf("Default ");
     }
     return 0;
}

Output:

Case 2

Why didn’t I use break statement after default?
The control would itself come out of the switch after default so I didn’t use it, however if you want to use the break after default then you can use it, there is no harm in doing that.

Few Important points regarding Switch Case

1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any integer value after case keyword. Also, case doesn’t need to be in an ascending order always, you can specify them in any order as per the need of the program.

2) You can also use characters in switch case. for example –

#include <stdio.h>
int main()
{
     char ch='b';
     switch (ch)
     {
         case 'd':
            printf("CaseD ");
            break;
         case 'b':
            printf("CaseB");
            break;
         case 'c':
            printf("CaseC");
            break;
         case 'z':
            printf("CaseZ ");
            break;
         default:
            printf("Default ");
    }
    return 0;
}

Output:

CaseB

3) The expression provided in the switch should result in a constant value otherwise it would not be valid.
For example:
Valid expressions for switch –

switch(1+2+23)
switch(1*2+3%4)

Invalid switch expressions –

switch(ab+cd)
switch(a+b+c)

4) Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
5) Duplicate case values are not allowed. For example, the following program is incorrect:
This program is wrong because we have two case ‘A’ here which is wrong as we cannot have duplicate case values.

#include <stdio.h>
int main()
{
     char ch='B';
     switch (ch)
     {
         case 'A':
            printf("CaseA");
            break;
         case 'A':
            printf("CaseA");
            break;
         case 'B':
            printf("CaseB");
            break;
         case 'C':
            printf("CaseC ");
            break;
         default:
            printf("Default ");
    }
    return 0;
}

6) The default statement is optional, if you don’t have a default in the program, it would run just fine without any issues. However it is a good practice to have a default statement so that the default executes if no case is matched. This is especially useful when we are taking input from user for the case choices, since user can sometime enter wrong value, we can remind the user with a proper error message that we can set in the default statement.

❮ PreviousNext ❯

Top Related Articles:

  1. C – while loop in C programming with example
  2. If statement in C programming with example
  3. C – loops in C programming with examples
  4. C – Pointer to Pointer (Double Pointer) with example
  5. User-defined function in C with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. judith cherono says

    February 15, 2016 at 6:41 PM

    write a c program using switch case statement to output the following
    2*3=6
    2+3=5
    4+6=10
    4/5=0.8

    Reply
    • RizanaParvin says

      March 27, 2019 at 9:52 AM

      #include <stdio.h>
      void main()
      {
      char operator;
      int num1,num2;
      printf(“\n Enter the operator (+, -, *, /):”);
      scanf(“%c”,&operator);
      printf(“\n Enter the Two numbers:”);
      scanf(“%d%d”,&num1,&num2);
      switch (operator)
      {
      case ‘+’:
      printf(“%d+%d=%d”,num1,num2,num1+num2);
      break;
      case ‘-‘:
      printf(“%d-%d=%d”,num1,num2,num1-num2);
      break;
      case ‘*’:
      printf(“%d*%d=%d”,num1,num2,num1*num2);
      break;
      case ‘/’:
      printf(“%d / %d = %d”,num1,num2,num1/num2);
      break;
      default:
      printf(“\n Enter the operator only”);
      break;

      }
      }

      Reply
      • Muhammad Aliyan says

        September 3, 2021 at 2:36 PM

        Thanks for your answer!

        Reply
    • Nana Percy says

      July 12, 2019 at 6:18 PM

      Please can I get the soluton to this question?

      Reply
  2. teboho says

    February 25, 2016 at 1:07 AM

    wow that’s a beautiful explanation

    Reply
  3. vikash tiwari says

    April 2, 2016 at 8:17 AM

    wow! how smoothly you explain it , it’s great.

    Reply
  4. Syed Minhaj Hussain says

    April 7, 2016 at 6:42 AM

    Using Switch statement, write a program that displays the following menu for the food items available to take order from the customer:
    • B= Burger
    • F= French Fries
    • P= Pizza
    • S= Sandwiches
    The program inputs the type of food and quantity. It finally displays the total charges for the order according to following criteria:
    • Burger = Rs. 200
    • French Fries= Rs. 50
    • Pizza= Rs. 500
    • Sandwiches= Rs. 150

    Reply
  5. FARZANA says

    December 12, 2018 at 5:02 PM

    #include <stdio.h>

    int main()
    {
    printf(“B=BURGER\nF=FRENCH FRY\nP=PIZZA\nS=SANDWICHES\n”);

    char ss;
    int n,x;
    scanf(“%c”,&ss);

    switch(ss)
    {
    case ‘B’:
    scanf(“%d”,&n);
    x= n*200;
    printf(“Burger=Rs %d”,x);

    case ‘F’:
    scanf(“%d”,&n);
    x= n*50;
    printf(“Burger=Rs %d”,x);

    case ‘P’:
    scanf(“%d”,&n);
    x= n*500;
    printf(“Burger=Rs %d”,x);

    case ‘S’:
    scanf(“%d”,&n);
    x= n*150;
    printf(“Burger=Rs %d”,x);
    }
    }

    Reply
  6. Nandlal Dnyaneshwar Patil says

    August 3, 2019 at 6:56 PM

    Answer to the question asked by Syed Minhaj Hussain:

    #include <stdio.h>
    int main()
    {

    int b,f,p,s,Burger,French,Pizza,Sandwiches;
    char ch,B,F,P,S;
    printf(“b1=Burger\n2=French Fries\n3=pizza\n4=Sandwiches\n”);
    printf(“Enter your order \nplease Enter the choice 1,2,3,4\n”);
    scanf(“%d”,&ch);
    switch(ch)

    {
    case 1:
    printf(“your order is Burger\n”);
    printf(“please enter your quantity “);

    scanf(“%d”,&b);
    Burger=200*b;

    printf(“your total charges is: %d”,Burger);
    break;
    case 2:
    printf(“your order is French \n”);
    printf(“please enter your quantity “);

    scanf(“%d”,&f);
    French=50*f;

    printf(“your total charges is: %d”,French);
    break;
    case 3:
    printf(“your order is Pizza\n”);
    printf(“please enter your quantity “);

    scanf(“%d”,&p);
    Pizza=500*p;

    printf(“your total charges is: %d”,Pizza);
    break;
    case 4:
    printf(“your order is Sandwiches\n”);
    printf(“please enter your quantity “);

    scanf(“%d”,&s);
    Sandwiches=150*s;

    printf(“your total charges is: %d”,Sandwiches);
    break;

    default:
    printf(“invalid your choice”);

    break;
    }
    }
    /*D:\cp>a/.out
    b1=Burger
    2=French Fries
    3=pizza
    4=Sandwiches
    Enter your order
    please Enter the choice 1,2,3,4
    4
    your order is Sandwiches
    please enter your quantity 6
    your total charges is: 900
    D:\cp>a/.out
    b1=Burger
    2=French Fries
    3=pizza
    4=Sandwiches
    Enter your order
    please Enter the choice 1,2,3,4
    6
    invalid your choice*/

    Reply
  7. dhruv says

    September 11, 2019 at 4:44 PM

    can we write an expression in case

    like
    case 1:

    so in case of 1 can we write an expression?

    Reply
    • Chaitanya Singh says

      October 7, 2019 at 12:21 PM

      Yes we can, see the point no 3 above in the important notes section. Expressions are allowed in case.

      Reply
    • morizonrobert says

      August 15, 2021 at 5:40 AM

      Yes but only if you want to output the expression….

      Reply
  8. Padmanavo says

    September 7, 2021 at 6:36 AM

    Can anyone please show how to use switch case system in a user defined function ?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap