Question discussion

What is the output of the following C program?#include int main() { int x = 2; switch (x) { case 1: printf("A"); case 2: printf("B"); case…

1552Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int main() {
    int x = 2;
    switch (x) {
        case 1: printf("A");
        case 2: printf("B");
        case 3: printf("C"); break;
        default: printf("D");
    }
    return 0;
}
  • AB
  • BABCD
  • CBCD
  • DBC
Correct answer: D

Explanation

Control enters at case 2 and falls through to case 3, where break stops it, so B and C are printed.

Discussion 0

Nobody has replied yet. Start the discussion!