Switch case in C: solved exercise with interactive menu

Switch case in C: solved exercise step by step

If you searched for a solved switch case exercise in C, here is the most common pattern: an interactive menu with several options and a default case for invalid input.

switch is the natural alternative to a chained if-else when comparing the same integer (or character) variable against multiple constant values.

Problem statement

Write a program that implements a basic console calculator. The user selects an operation from a numbered menu (1–4) and enters two operands. The program prints the result and warns on invalid options or division by zero.

C solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>

int main(void) {
    int option;
    double a, b;

    printf("=== Calculator ===\n");
    printf("1. Add\n");
    printf("2. Subtract\n");
    printf("3. Multiply\n");
    printf("4. Divide\n");
    printf("Option: ");
    scanf("%d", &option);

    printf("Enter two numbers: ");
    scanf("%lf %lf", &a, &b);

    switch (option) {
        case 1:
            printf("%.2f + %.2f = %.2f\n", a, b, a + b);
            break;
        case 2:
            printf("%.2f - %.2f = %.2f\n", a, b, a - b);
            break;
        case 3:
            printf("%.2f * %.2f = %.2f\n", a, b, a * b);
            break;
        case 4:
            if (b == 0.0) {
                printf("Error: division by zero\n");
            } else {
                printf("%.2f / %.2f = %.2f\n", a, b, a / b);
            }
            break;
        default:
            printf("Invalid option\n");
            break;
    }

    return 0;
}

Expected output

With inputs 1, 3.0, 4.0:

1
2
3
4
5
6
7
8
=== Calculator ===
1. Add
2. Subtract
3. Multiply
4. Divide
Option: 1
Enter two numbers: 3.0 4.0
3.00 + 4.00 = 7.00

Common mistakes

  • Forgetting break at the end of each case: without it, execution falls through to the next case.
  • Using non-constant or floating-point expressions as case labels (does not compile).
  • Omitting default and leaving invalid input unhandled.
  • Trying to use switch with strings: it only works with integer types (int, char, enum).

When to use switch instead of if-else

SituationPrefer
Comparing one variable against many constantsswitch
Range checks or complex expressionsif-else
Comparing stringsif-else with strcmp
Two or three brancheseither

Practical use

switch appears in:

  • console menus and state machines,
  • command or keystroke processing,
  • decoding error codes or operation codes.

Guided practice and next step

If you want a complete path with progressive difficulty:

FAQ

What is fall-through in switch?

If there is no break at the end of a case, execution continues into the next case even if its label does not match. This can be intentional to share code between cases, but is usually a bug.

Can switch compare strings in C?

No. switch only works with integer types (int, char, short, long, enum). To compare strings use if-else with strcmp.

Is switch faster than if-else?

With many cases, many compilers compile switch as a jump table, which can be faster than a chain of if-else. With few branches the difference is negligible.