C course menu
switch Statement in C: case, break, default and Fall-through
Learn the C switch statement: matching an integer or character against case labels, break and fall-through, grouping cases, the default case, what types switch accepts, and a menu-driven program example.
On this page
switch compares one integer value against a list of constant values (case labels) and jumps to the matching one. It is often clearer than a long if...else if chain.
Basic switch#
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Another day\n");
}
return 0;
}Wednesday- The expression in
switch (...)must be an integer type, which includescharandenum. Strings and decimals are not allowed. - Each
casemust be a constant known at compile time. defaultruns when no case matches.
break and fall-through#
Without break, execution continues into the next case, even if its label does not match:
int level = 2;
switch (level) {
case 1:
printf("Level 1 bonus\n");
case 2:
printf("Level 2 bonus\n");
case 3:
printf("Level 3 bonus\n");
}Level 2 bonus
Level 3 bonusForgetting break is the classic switch bug. GCC's -Wimplicit-fallthrough (part of -Wextra) warns about it. When fall-through is intended, say so with a comment such as /* fall through */ or, in C23, the [[fallthrough]]; attribute.
Grouping cases#
Several labels can share code:
char grade = 'B';
switch (grade) {
case 'A':
case 'B':
printf("Well done\n");
break;
case 'C':
printf("Good\n");
break;
default:
printf("Keep practising\n");
}Well doneswitch with enums#
switch pairs naturally with enums. With -Wall, GCC warns if a switch on an enum misses one of its values (when there is no default):
#include <stdio.h>
enum Status { PLACED, SHIPPED, DELIVERED };
const char *label(enum Status s) {
switch (s) {
case PLACED:
return "We have your order";
case SHIPPED:
return "On the way";
case DELIVERED:
return "Delivered";
}
return "Unknown";
}
int main(void) {
printf("%s\n", label(SHIPPED));
return 0;
}On the wayA menu-driven program#
switch is often used for menus:
#include <stdio.h>
int main(void) {
int choice;
printf("1. Balance\n2. Deposit\n3. Exit\nChoose: ");
if (scanf("%d", &choice) != 1) return 1;
switch (choice) {
case 1:
printf("Your balance is 5000\n");
break;
case 2:
printf("Deposit selected\n");
break;
case 3:
printf("Goodbye\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}1. Balance
2. Deposit
3. Exit
Choose: 2
Deposit selectedswitch or if?#
Use switch for one integer or character compared with several fixed values. Use if for ranges (marks >= 90), several variables, strings (compared with strcmp) or decimals.
Practice#
Practice exercise
Write a calculator that applies an operator character (+, -, *, /) to two numbers with a switch, printing an error for division by zero and unknown operators. Test 12 * 4, 9 / 0 and 5 % 2.
Show solution
#include <stdio.h>
void calculate(double a, char op, double b) {
switch (op) {
case '+': printf("%g\n", a + b); break;
case '-': printf("%g\n", a - b); break;
case '*': printf("%g\n", a * b); break;
case '/':
if (b == 0) printf("Cannot divide by zero\n");
else printf("%g\n", a / b);
break;
default:
printf("Unknown operator '%c'\n", op);
}
}
int main(void) {
calculate(12, '*', 4);
calculate(9, '/', 0);
calculate(5, '%', 2);
return 0;
}48
Cannot divide by zero
Unknown operator '%'%g prints a double without unnecessary trailing zeros.
Key takeaway#
Finished this lesson?