C Reference ยท Keywords and Statements
The switch statement
Reference for the C switch statement: case labels, default, fall-through, the rules for case values, and common mistakes such as a missing break.
switch selects one of many entry points based on the value of an integer expression. Execution jumps to the case label whose constant equals the expression, or to default if none matches, and then keeps running through the following labels until a break, a return or the closing brace. That behavior is called fall-through.
The controlling expression must have an integer type (including char, enumerations and bool). Strings, floating-point numbers and pointers are not allowed.
switch (expression) { case 1: /* ... */ break; case 2: case 3: /* shared by 2 and 3 */ break; default: /* anything else */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
switch ( expression ) statement case constant-expression : statement default : statement
| Part | Description |
|---|---|
expression |
Integer expression. It undergoes the integer promotions. |
case value |
An integer constant expression (a literal, an enumerator, constexpr in C23). Each value may appear only once. |
default |
Optional label chosen when no case matches. It can appear anywhere in the body. |
Returns: nothing; switch is a statement.
Errors / undefined behavior: duplicate case values or non-constant labels are compile errors. Jumping past an initialized variable declaration into its scope (or into a variable length array's scope) is a constraint violation or undefined behavior, so wrap per-case variables in braces.
Usage notes
- Always end a case with
break,returnor[[fallthrough]];(C23). Without one, execution continues into the next label;-Wimplicit-fallthroughfinds the accidental ones. - Grouping: stacked labels (
case 1: case 7:) share one body, and this is the normal, accepted form of fall-through. - Declarations after a label are not allowed before C23 (a label must precede a statement). Open a block:
case 1: { int t = f(); ... break; }. - No ranges in standard C.
case 1 ... 5:is a GCC/Clang extension; write the labels out or useif. - No strings. Compare strings with
strcmpin anifchain. breakinside a loop inside a switch leaves only the nearest enclosing loop or switch. See break and continue.- Missing
defaulton anenumis a deliberate style choice: compilers then warn when you forget an enumerator. - Performance: compilers may build a jump table or a binary search; the choice is theirs, so do not assume either.
Related
ifandelse,breakandcontinue,enum, attributes- Lessons: switch, Enumerations