case Constant case labels in switch
case Constant case labels in switch
case needs an integer constant expression, converted to the switch's promoted type; no duplicates. const variables fail; consecutive labels share code.
| Category | Control flow |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
case 1: case 2: /* Multiple cases share code */
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
small
🏷️ Related control-flow keywords
📚 Related Tutorials
❓ FAQ
QCan const int n = 1 be used as case n?
ANo. In C, a const object is not an integer constant expression. Use enum { n = 1 }, an object-like macro, or case 1 directly, while respecting the identifier namespace.
QDoes standard C support range labels such as case 1 ... 3?
ANo. That is a compiler extension, not C89, C99, or C11 syntax. For portable code, list each constant label or use conditional statements instead.
QHow can multiple case labels share the same code?
APlace the labels consecutively, then write the shared statements after case 1: case 2:. A match for either label enters the same location; break, return, and similar statements still determine subsequent fall-through.