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.

The default keyword in C serves as a fallback execution path within a switch statement or as the default type association in a C11 _Generic selection. Within a switch statement, the default label acts as the target for control flow when the evaluated integer expression of the control block does not equal any of the defined case constant expressions.
#include <stdio.h>

// _Generic selection usage (C11+)
#define TYPE_CHECK(x) _Generic((x), \
    int: 1, \
    default: 0 \
)

int main(void) {
    int expression = 5;
    
    // switch statement usage
    switch (expression) {
        case 1:
            printf("One\n");
            break;
        default:
            printf("Default fallback\n");
            break;
    }

    int is_int = TYPE_CHECK(3.14);
    printf("Is int: %d\n", is_int); // Outputs 0

    return 0;
}

Technical Characteristics

Uniqueness A single switch statement or _Generic selection may contain a maximum of one default label or association. Multiple default labels can legally exist within the same lexical scope, provided they belong to strictly separate switch statements or _Generic selections. Optionality The default keyword is not required. If a switch statement lacks a default label and the evaluated expression does not match any case label, the control flow simply bypasses the entire switch statement, and no internal statements are executed. In a _Generic selection, omitting default when no specified type matches the controlling expression results in a compilation error. Position Independence and Evaluation The C standard does not mandate the physical placement of the default label within a switch statement. It can be positioned at the beginning, middle, or end. case expressions are integer constant expressions evaluated at compile time. At runtime, the controlling expression of the switch is evaluated exactly once, and control is transferred directly to the matching label (often optimized via a jump table). If no case constant matches the evaluated expression, control transfers directly to the default label, regardless of its lexical position.
#include <stdio.h>

int main(void) {
    int value = 2;
    
    switch (value) {
        default: 
            printf("Default executed\n"); 
            break;
        case 1: 
            printf("Case 1 executed\n"); 
            break;
    }
    
    return 0;
}
Fallthrough Behavior Inside a switch statement, the default label is subject to the same lexical control flow rules as case labels. If the default block is executed and is not explicitly terminated by a jump statement (such as break, return, or goto), execution will “fall through” into the lexically subsequent case block, ignoring that block’s constant expression.
#include <stdio.h>

int main(void) {
    int value = 5;
    
    switch (value) {
        case 1:
            printf("Case 1\n");
            break;
        default:
            printf("Default\n");
            // Missing break; execution falls through to case 2
        case 2:
            printf("Case 2\n");
            break;
    }
    
    return 0;
}

Scope and Constraints

  • When used as a control flow label, default must be associated with a switch statement. However, the C standard dictates that a switch takes a statement, which does not strictly have to be a compound statement (block). A single statement prefixed by default is perfectly valid C.
#include <stdio.h>

int main(void) {
    int x = 10;
    // Valid C: switch with a single non-compound statement
    switch (x) default: puts("fallback");
    return 0;
}
  • In C89 through C17, a label must strictly precede a statement. If the default label is the final item in a switch block, it must be followed by a null statement (;) or a jump statement (e.g., break;) to satisfy the compiler’s syntax rules.
  • C23 introduced free-standing labels (N2508), which allows the default label to appear at the end of a compound statement without requiring a trailing statement.
Master C with Deep Grasping Methodology!Learn More