Skip to content
Laviyx Learn

Try

C course menu

switch Statement in C: case, break, default and Fall-through

Learn the C switch statement: matching an integer or character against case labels, break and fall-through, grouping cases, the default case, what types switch accepts, and a menu-driven program example.

Beginner9 min readUpdated
On this page

switch compares one integer value against a list of constant values (case labels) and jumps to the matching one. It is often clearer than a long if...else if chain.

Basic switch#

C
#include <stdio.h>

int main(void) {
    int day = 3;
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Another day\n");
    }
    return 0;
}
Output
Wednesday
  • The expression in switch (...) must be an integer type, which includes char and enum. Strings and decimals are not allowed.
  • Each case must be a constant known at compile time.
  • default runs when no case matches.

break and fall-through#

Without break, execution continues into the next case, even if its label does not match:

C
int level = 2;
switch (level) {
    case 1:
        printf("Level 1 bonus\n");
    case 2:
        printf("Level 2 bonus\n");
    case 3:
        printf("Level 3 bonus\n");
}
Output
Level 2 bonus
Level 3 bonus

Forgetting break is the classic switch bug. GCC's -Wimplicit-fallthrough (part of -Wextra) warns about it. When fall-through is intended, say so with a comment such as /* fall through */ or, in C23, the [[fallthrough]]; attribute.

Grouping cases#

Several labels can share code:

C
char grade = 'B';
switch (grade) {
    case 'A':
    case 'B':
        printf("Well done\n");
        break;
    case 'C':
        printf("Good\n");
        break;
    default:
        printf("Keep practising\n");
}
Output
Well done

switch with enums#

switch pairs naturally with enums. With -Wall, GCC warns if a switch on an enum misses one of its values (when there is no default):

C
#include <stdio.h>

enum Status { PLACED, SHIPPED, DELIVERED };

const char *label(enum Status s) {
    switch (s) {
        case PLACED:
            return "We have your order";
        case SHIPPED:
            return "On the way";
        case DELIVERED:
            return "Delivered";
    }
    return "Unknown";
}

int main(void) {
    printf("%s\n", label(SHIPPED));
    return 0;
}
Output
On the way

A menu-driven program#

switch is often used for menus:

C
#include <stdio.h>

int main(void) {
    int choice;
    printf("1. Balance\n2. Deposit\n3. Exit\nChoose: ");
    if (scanf("%d", &choice) != 1) return 1;

    switch (choice) {
        case 1:
            printf("Your balance is 5000\n");
            break;
        case 2:
            printf("Deposit selected\n");
            break;
        case 3:
            printf("Goodbye\n");
            break;
        default:
            printf("Invalid choice\n");
    }
    return 0;
}
Output
1. Balance
2. Deposit
3. Exit
Choose: 2
Deposit selected

switch or if?#

Use switch for one integer or character compared with several fixed values. Use if for ranges (marks >= 90), several variables, strings (compared with strcmp) or decimals.

Practice#

Practice exercise

Write a calculator that applies an operator character (+, -, *, /) to two numbers with a switch, printing an error for division by zero and unknown operators. Test 12 * 4, 9 / 0 and 5 % 2.

Show solution
C
#include <stdio.h>

void calculate(double a, char op, double b) {
    switch (op) {
        case '+': printf("%g\n", a + b); break;
        case '-': printf("%g\n", a - b); break;
        case '*': printf("%g\n", a * b); break;
        case '/':
            if (b == 0) printf("Cannot divide by zero\n");
            else printf("%g\n", a / b);
            break;
        default:
            printf("Unknown operator '%c'\n", op);
    }
}

int main(void) {
    calculate(12, '*', 4);
    calculate(9, '/', 0);
    calculate(5, '%', 2);
    return 0;
}
Output
48
Cannot divide by zero
Unknown operator '%'

%g prints a double without unnecessary trailing zeros.

Key takeaway#

Finished this lesson?