`switch` — C Keyword
`switch` — C Keyword
The switch keyword in C: selects among multiple execution paths based on an integer value.
`switch` — C Keyword
The switch keyword in C: selects among multiple execution paths based on an integer value.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
switch (C)Selects among multiple execution paths based on the value of an integer expression.
switch (expression) {
case constant1: statements; break;
case constant2: statements; break;
default: statements;
}
#include <stdio.h>
const char* day_name(int d) {
switch (d) {
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
default: return "Weekend";
}
}
int main(void) {
printf("%s\n", day_name(3)); // Wednesday
printf("%s\n", day_name(7)); // Weekend
return 0;
}
break, execution falls through to the next case.switch expression must be an integer type.switchint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}