Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

switch — selection by integer value

switch transfers control to a matching case label or the optional default. Labels do not stop execution; break exits the statement.

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

Syntax

switch (expression) {
    case constant_expression:
        statements;
        break;
    default:
        statements;
}

switch requires an expression of integer type. After integer promotions, its value is compared with the case values. These must be integer constant expressions, unique after conversion to the promoted type.

The optional default label handles no match; without it, the body is skipped. Execution continues through subsequent labels until control flow is interrupted, for example by break. Labels do not create separate scopes. In the example, case 2 and case 3 share code.

Examples

#include <stdio.h>

int main(void)
{
    int option = 2;

    switch (option) {
        case 1:
            puts("one");
            break;
        case 2:
        case 3:
            puts("two or three");
            break;
        default:
            puts("other");
            break;
    }

    return 0;
}

Result

two or three

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