Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

default — fallback alternative

The default keyword provides a fallback in switch or _Generic: respectively for an unmatched value or type.

Version: C23; repository compiler flag, production runtime not attested

Syntax

switch (expression) {
    case constant_expression: statement; break;
    default: statement; break;
}
_Generic(expression, type_name: expression, default: expression)

In switch, the default: label is the jump target when no case matches the controlling value after conversions. It is optional; without a match or default, the body is skipped.

Each switch allows at most one default, not necessarily last. It does not stop execution: control can fall through to subsequent labels. break exits the nearest enclosing loop or switch.

In _Generic, default: expression selects an expression when no type is compatible. At most one is allowed; without it, a type must match. The controlling expression and unselected expressions are not evaluated.

Examples

#include <stdio.h>

int main(void)
{
    int value = 7;

    switch (value) {
    case 1:
        puts("one");
        break;
    default:
        puts("other");
        break;
    }

    puts(_Generic(value, double: "double", default: "not double"));
    return 0;
}

Result

other
not double

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources