ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int x = 2, r = 0;
    switch (x) {
        case 1: r += 1;
        case 2: r += 10;
        case 3: r += 100; break;
        default: r += 1000;
    }
    printf("%d", r);
}
  1. A. 10
  2. B. 100
  3. C. 1110
  4. D. 110

Correct answer: D. 110

Execution starts at case 2 and falls through into case 3 until the break, so r = 10 + 100 = 110. The default branch is not reached.

More C Programming questions

Exams that ask this topic