ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int c = 0;
    for (int i = 0; i < 5; i++) {
        switch (i) {
            case 2: break;
            default: c++;
        }
    }
    printf("%d", c);
}
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: C. 4

The break exits only the switch, not the loop, so the loop body increments c for every i except 2, which gives 4.

More C Programming questions

Exams that ask this topic