Skip to main content
A case label is a specialized jump target used exclusively within the body of a switch statement to direct control flow. It specifies an integer constant expression that the compiler compares against the evaluated result of the switch statement’s controlling expression. When a match occurs, execution jumps directly to the statement immediately following the label.

Technical Constraints

  • Constant Expression Requirement: The expression following the case keyword must evaluate to an integer type (int, char, enum) at compile time. Variables, floating-point values, string literals, and function calls are strictly prohibited.
  • Uniqueness: Every case label within a single switch block must evaluate to a unique integer value. Duplicate values will trigger a compilation error.
  • Type Promotion: According to the C standard, integer promotions are first performed on the switch statement’s controlling expression. Subsequently, each case constant expression is converted to the promoted type of the controlling expression before the comparison occurs.
  • Scope and Placement: case labels have no lexical scope of their own. They must be enclosed within a switch statement, but C syntax permits them to be nested within other control structures inside that switch (a mechanic famously exploited by Duff’s Device).

Execution Mechanics

A case label acts purely as an entry point; it does not dictate an exit point. Once control jumps to a case label, execution proceeds sequentially. It will bypass subsequent case and default labels, executing their associated statements in a behavior known as “fallthrough.” To terminate the execution path, an explicit jump statement (such as break, return, or goto) must be invoked.

Lexical Rules Regarding Declarations

Historically in C (prior to C23), a label can only precede a statement, not a declaration. If a variable must be declared immediately following a case label, the execution path must be wrapped in a compound statement (a block) to satisfy the grammar rules. This compound block approach is universally valid across all versions of C. Alternatively, an empty statement (;) can be placed immediately after the colon to satisfy the label requirement. However, because C89/C90 strictly requires all declarations to precede statements within a block, this empty statement workaround is only valid in C99 and later.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More