switch / case
Basic Syntax
#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("Some other day\n");
break;
}
// Output: Wednesday
return 0;
}case label whose constant matches the switch expression. The optional default label runs when no case matches; it can appear anywhere in the switch but is conventionally placed last.Fallthrough and Why break Is Required
break statement.int n = 1;
// BUGGY: missing break statements
switch (n) {
case 1:
printf("one\n"); // prints
case 2:
printf("two\n"); // ALSO prints -- falls through from case 1!
case 3:
printf("three\n"); // ALSO prints -- falls through from case 2!
break;
default:
printf("other\n");
}
// Output:
// one
// two
// threebreak is one of the most classic bugs in C. Every case block that should not continue into the next one needs its own break (or a return if inside a function). Compilers such as GCC/Clang can warn about suspicious fallthrough with -Wimplicit-fallthrough — treat that warning seriously.Intentional Fallthrough: Grouping Cases
Fallthrough is occasionally useful on purpose: stacking multiple case labels with no code between them lets several values share the same block.
char grade = 'B';
switch (grade) {
case 'A':
case 'B':
case 'C':
printf("Passing grade\n");
break;
case 'D':
case 'F':
printf("Failing grade\n");
break;
default:
printf("Invalid grade\n");
break;
}
// Output: Passing grade/* fallthrough */ so future readers know it is not a forgotten break.switch Only Works on Integer Types
switch: the controlling expression and every case label must be a constant integer expression — that includes int, char, and enum values. C has no built-in support for switching on strings, unlike some other languages that do allow string switch statements.char *name = "bob";
// This does NOT compile in C: you cannot switch on a char* / string.
switch (name) {
case "bob": // ERROR: case label does not reduce to an integer constant
printf("Hi Bob\n");
break;
}
// Correct approach: use if/else with strcmp() instead.
if (strcmp(name, "bob") == 0) {
printf("Hi Bob\n");
} else if (strcmp(name, "alice") == 0) {
printf("Hi Alice\n");
}switch on a char *. C compares strings by content, not by a single machine value, soswitch cannot dispatch on them at all. Use an if / else if ladder with strcmp() (from <string.h>) instead.Feature | C switch |
|---|---|
Valid controlling types | int, char, enum (any integer-promotable type) |
Switching on strings | Not supported |
Falls through by default | Yes -- break is required to stop |
Multiple values, one block | Stack case labels with no code between them |
Unmatched value | default label runs if present, otherwise nothing runs |
Key Points
switch compares one value against a series of exact constant case labels.
Without break, execution falls through into the next case -- this is one of the most classic C bugs.
Stacking case labels with no code between them is the correct way to group values that share behavior.
switch only works on integer-compatible types (int, char, enum); it cannot switch on strings.
For string comparisons, use an if/else ladder with strcmp() instead of switch.