Switching on Strings

How to Write a Switch Case in C

How to write a switch case in C, step by step: the syntax, the role of break and default, a full working program, and common mistakes to avoid.

On this page
  1. The basic syntax
  2. A working example
  3. Why break matters
  4. Grouping cases and using default
  5. Declaring a variable inside a case
  6. Common mistakes
  7. Using it in C++

The basic syntax

The switch case statement in C runs one of several blocks, based on the value of an expression. Here is the general form:

switch (expression) {
    case constant1:
        statements;
        break;
    case constant2:
        statements;
        break;
    default:
        statements;
        break;
}

The parts work like this:

  • The expression is evaluated once. In C it must have an integer type, such as int, char or an enum value. A float, a double or a string is not allowed.
  • Each case label must be a constant integer expression, such as 1, 'a' or an enum constant. Two case labels in the same switch cannot have the same value.
  • The break ends the switch. Without it, execution continues into the next case.
  • The default label is optional. It runs when no case matches, and most code puts it last.

A working example

This program reads a menu choice and prints a matching message:

#include <stdio.h>

int main(void) {
    int choice = 2;

    switch (choice) {
        case 1:
            printf("Option one\n");
            break;
        case 2:
            printf("Option two\n");
            break;
        case 3:
            printf("Option three\n");
            break;
        default:
            printf("Unknown option\n");
            break;
    }

    return 0;
}

Output:

Option two

Change choice to 4 and the default case runs instead, printing Unknown option. In a real program you would read choice with scanf(), and check that the read succeeded before you switch on it.

Why break matters

If a case has no break, execution falls through into the next case. Sometimes that is intended, but more often it is a bug:

switch (choice) {
    case 1:
        printf("Option one\n");
    case 2:
        printf("Option two\n");
        break;
}

With choice set to 1, this prints both Option one and Option two. Many compilers can warn about implicit fall-through when you turn on extra warnings, so compile with warnings enabled. If you do want fall-through, add a comment that says so.

Grouping cases and using default

Several labels can share one block by stacking them, with no statements between them:

char letter = 'e';

switch (letter) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        printf("Vowel\n");
        break;
    default:
        printf("Consonant or other character\n");
        break;
}

The default label catches any value that no case matches, so it is a good place to handle unexpected input.

Declaring a variable inside a case

Put braces around a case body when you need a variable. In older C standards, a declaration cannot come directly after a case label. A variable declared in one case without braces also stays in scope for the rest of the switch:

switch (choice) {
    case 1: {
        int total = 10 * 2;
        printf("%d\n", total);
        break;
    }
    default:
        break;
}

Common mistakes

  • Forgetting break. This causes fall-through, as shown above.
  • Switching on a float or a string. Only integer types are allowed. To branch on text in C, compare with strcmp() in an if chain instead. For ranges, such as a score between 50 and 59, use the if else statement syntax in C, because a case label has to be a single value.
  • Duplicate case values. Two labels with the same value are a compile error.
  • Expecting break to leave a loop. Inside a loop, break only leaves the switch. The loop keeps going. Use continue if you want to skip to the next loop iteration.

Using it in C++

C++ uses the same syntax, so this guide applies there too. One difference shows up with variables. In C++, jumping past a declaration that has an initializer into a later case is a compile error. The braces in the example above avoid that problem.

To sum up, a switch needs an integer-type value, each case is a constant label, and a break at the end of each case keeps the branches separate. Add a default case for anything you did not expect.

The complete guide to switching on strings

How to Switch on a String in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

std::array in C++

A complete guide to std::array in C++: declaring, initializing, reading elements, looping, passing to functions, and how it compares to C-style arrays.

std::array 5 min read