Question 12
Back to Exercise
What is the output of the following program, compiled as C23?
#include <stdio.h>
void access(int level) {
switch (level) {
case 3:
printf("delete ");
[[fallthrough]];
case 2:
printf("write ");
[[fallthrough]];
case 1:
printf("read");
break;
default:
printf("none");
}
printf("\n");
}
int main(void) {
access(2);
access(4);
return 0;
}
- A write read none Correct
- B write none
- C delete write read none
- D write read delete write read
Correct Answer: Option A
Level 2 enters at case 2 and deliberately falls into case 1: "write read". Level 4 matches no case, so default prints "none". [[fallthrough]] changes nothing at run time – it only tells the compiler the fall-through is intended.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!