LearnFrenzy
Sign in Take a free test

Control Instructions · Predict the Output · Question 4

default written first in a switch – output Answer, explanation & discussion

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

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int n = 5;
    switch (n) {
    default: printf("D ");
    case 1:  printf("1 ");
             break;
    case 2:  printf("2 ");
    }
    printf("\n");
    return 0;
}
  • A D 1 Correct
  • B D
  • C D 1 2
  • D 1
Correct Answer: Option A

No case matches 5, so control jumps to default. It has no break, so it falls through into case 1, which prints 1 and breaks.

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