ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int n = 10, c = 0;
int k = (n + 3) / 4;
switch (n % 4) {
case 0: do { c++;
case 3:      c++;
case 2:      c++;
case 1:      c++;
        } while (--k > 0);
}
printf("%d %d", c, k);
  1. A. 10 0
  2. B. 12 0
  3. C. 10 1
  4. D. 8 0

Correct answer: A. 10 0

This is Duff's device: case labels may sit inside the do-while body. k = 13 / 4 = 3 and n % 4 = 2, so control jumps to case 2 and the first pass does only two increments (c = 2). --k makes k 2 and the loop runs a full pass of four (c = 6), --k = 1 gives another (c = 10), and --k = 0 ends it. c equals n, as intended; 12 assumes three full passes.

More C Programming questions

Exams that ask this topic