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,charor an enum value. Afloat, adoubleor 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 anifchain 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,
breakonly leaves the switch. The loop keeps going. Usecontinueif 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.