LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 12

C23 [[fallthrough]] chain – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following program, compiled as C23?

main.c
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links