Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
case constant-expression: statement

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.
switch (x) {
    case 1: 
        /* Execution starts here if x == 1 */
        x = 10; 
        /* Falls through to case 2 unless a break is present */
    case 2: 
        x = 20;
        break;
}

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.
switch (x) {
    case 1: {
        /* Universally valid (C89+): Declaration inside a compound statement */
        int y = 5; 
        x = y;
        break;
    }
    case 2: ;      
        /* Valid only in C99+: Empty statement precedes declaration */
        int z = 10;
        x = z;
        break;
}
Master C with Deep Grasping Methodology!Learn More