The switch Statement in C
switch dispatches on an integer-like value (int, char, enum) to labeled case branches — cleaner than long == chains when one variable decides everything. Execution falls through from the matching case into every case below unless break stops it: occasionally useful for shared handling, usually a forgotten-break bug.
Cases must be compile-time constant expressions, and default catches unmatched values. Compilers warn on missing breaks (-Wimplicit-fallthrough) and unhandled enum values — the two classic switch mistakes as build messages.
Dispatch on one value
switch (day) jumps straight to case 3: — one expression tested against many constant labels.
break stops fallthrough
Without break, execution slides into the next case's statements. End every case with break (or return) unless sharing is deliberate.
default handles the rest
default: runs when nothing matched — the else of the switch world, ideal for invalid-input handling.
Constants only
Case labels must be compile-time constants — literals, enums, const-int expressions. Ranges and variables are not allowed.
#include <stdio.h> int main(void) { int day = 3; switch (day) { case 1: printf("Mon\n"); break; case 3: printf("Wed\n"); break; default: printf("other\n"); break; } return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.