C:C Switch-Case Statement: Syntax and Fallthrough
C•Switch Statement

C Switch-Case Statement: Syntax and Fallthrough

The C switch statement evaluates an integer expression and jumps directly to the matching case label.

Syntax

switch (int_expr) { case VAL1: // code break; default: // code }

Parameters & Values

Key components and acceptable values for this structure:

ParameterTypeDescription
breakStatementPrevents automatic execution of next case.

Ready-to-Run Code Example

Executable in Compilra

Run this complete, working example directly without installing C locally:

#include <stdio.h>

int main(void) {
    char protocolGrade = 'B';

    printf("Protocol Security Evaluation:\n");
    switch (protocolGrade) {
        case 'A':
            printf("  [EXCELLENT] Grade A: TLS 1.3 Strict\n");
            break;
        case 'B':
            printf("  [GOOD] Grade B: TLS 1.2+ Standard\n");
            break;
        case 'C':
        case 'D':
            printf("  [DEPRECATED] Grade C/D: Legacy cipher suites detected\n");
            break;
        default:
            printf("  [UNKNOWN] Unrecognized protocol grade\n");
            break;
    }

    return 0;
}
Expected Output:
Protocol Security Evaluation:
  [GOOD] Grade B: TLS 1.2+ Standard

How It Works: Step-by-Step

  • Lines 7-21: Evaluates character code against case labels.
  • Lines 14-16: Demonstrates intentional fallthrough combining C and D.

Key Concepts

  • switch statements in C only accept integer or character constants.
  • Without break, execution continues unconditionally into the next case.

Common Mistake: Using non-constant expressions in case labels

In C, case labels must be compile-time integer constants. Variables are not allowed.

❌ Incorrect
int x = 5;
switch(val) { case x: ... } // Compile Error: case label does not reduce to an integer constant
✅ Correct
switch(val) { case 5: ... }

Related C Tutorials & Compiler

Run and test C on Compilra

Execute this code snippet with pre-supplied standard input (stdin) in Compilra’s cloud compiler.

C Online Compiler