Switch-Case in C
The switch statement in C picks one block of code to run based on the value of a single integer or character expression. It is a cleaner alternative to a long if-else ladder when you are comparing one variable against many fixed values.
Basic syntax
switch (expression) {
case value1:
/* runs if expression == value1 */
break;
case value2:
/* runs if expression == value2 */
break;
default:
/* runs if no case matched */
break;
}
The expression must evaluate to an integer type — that includes int and char (a char is just a small integer). Each case label must be a constant, not a variable or a range.
A working example
#include <stdio.h>
int main(void) {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break; /* break stops the switch here */
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n"); /* this matches, prints, then breaks */
break;
default:
printf("Some other day\n");
break;
}
return 0;
}
The matching case runs; break then jumps out of the switch.
Why break matters: fall-through
If you forget break, C keeps running into the next case — this is called fall-through. Sometimes it is a bug, sometimes it is exactly what you want when several cases share the same action:
#include <stdio.h>
int main(void) {
char grade = 'B';
switch (grade) {
case 'A':
case 'B': /* both A and B fall through to here */
printf("Well done!\n");
break;
case 'C':
printf("You passed.\n");
break;
default:
printf("Keep practising.\n");
break;
}
return 0;
}
Here 'A' and 'B' deliberately share one block. When fall-through is intentional, add a comment so readers know it's not a missing break.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingThe default case
default is the catch-all, like the final else. It can appear anywhere in the switch, but by convention it goes last. It is optional, but including it makes your code handle unexpected values gracefully.
A practical menu
switch is a natural fit for menu-driven programs, where the user picks an option and you run the matching action:
#include <stdio.h>
int main(void) {
int choice;
printf("1) Add 2) Subtract 3) Quit\n");
printf("Choose: ");
if (scanf("%d", &choice) != 1) { /* guard against bad input */
return 1;
}
switch (choice) {
case 1:
printf("You chose Add.\n");
break;
case 2:
printf("You chose Subtract.\n");
break;
case 3:
printf("Goodbye!\n");
break;
default:
printf("Unknown option.\n"); /* catches everything else */
break;
}
return 0;
}
Pairing this with a do-while loop lets the menu repeat until the user chooses Quit.
switch vs if-else
- Use switch when one variable is tested against several constant values (menu choices, status codes, single characters).
- Use if-else for ranges (
score >= 75), floating-point tests, or compound conditions with&&/||. - A
switchcan be faster and clearer than a long chain ofelse if, because the reader sees at a glance that one variable drives every branch.
Common mistakes
- Forgetting
break, causing unintended fall-through into later cases. - Using a variable or range as a case label:
case x:orcase 1...5:are not valid standard C. Case labels must be compile-time constants. - Using a
floatordoubleinswitch (...): only integer types (includingchar) are allowed. - Duplicate case values: two
case 2:labels is a compile error. - Declaring a variable inside a case without braces: declarations that need initialization can clash across cases. Wrap a case body in
{ }if you declare variables there.
FAQ
Can I use strings in a switch? No. C switch only works on integer types. For strings, compare with strcmp inside if-else.
Does the order of cases matter? Not for correctness (each is matched by value), but keep them readable and put default last.
Keep going at the hub C Programming, and compare with If-Else in C and Functions in C.
Want to learn this properly? Join the waitlist for our C Programming course — taught in Jalgaon.
Want to learn this properly?
Join the waitlist for C Programming — beginner-friendly, project-first classes in Jalgaon.
Explore C ProgrammingFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in C
A beginner guide to arrays in C — declaring, initializing and indexing them, looping over elements, multidimensional arrays, and avoiding out-of-bounds errors.
Common C Errors & How to Fix Them
A troubleshooting guide to the most common C errors beginners face — from = vs ==, format mismatches and missing semicolons to segmentation faults — and how to fix each.
Dynamic Memory Allocation in C
A beginner guide to dynamic memory allocation in C — malloc, calloc, realloc and free, allocating at runtime, and preventing memory leaks.
