Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

case — a value label in switch

The case keyword marks an entry point in a switch statement for a particular integer constant value. A label does not end a branch: without a control transfer, execution continues into subsequent code.

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

Syntax

case constant-expression:

case creates a label inside switch and belongs to the nearest enclosing switch statement. The form case constant-expression: requires an integer constant expression, such as 1, 'A', or an enumeration constant; a value computed only at runtime is not sufficient.

The controlling expression of switch must have integer type. It undergoes integer promotions, and label values are converted to the resulting type. Two case values in the same switch must not be equal after this conversion. A match transfers control past the corresponding label; without a match, the optional default is selected, or the body is skipped if none exists.

Labels neither stop execution nor create separate scopes. Without break or another control transfer, execution continues, including through subsequent labels. break exits the nearest enclosing loop or switch statement; in the example it exits switch. The adjacent case 1: and case 2: share the code that prints small.

Examples

#include <stdio.h>

int main(void)
{
    int choice = 2;

    switch (choice) {
    case 1:
    case 2:
        puts("small");
        break;
    case 3:
        puts("three");
        break;
    default:
        puts("other");
        break;
    }

    return 0;
}

Result

small

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