ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
int n = 3, s = 0;
while (n--) s += n;
printf("%d %d", s, n);
  1. A. 3 0
  2. B. 6 -1
  3. C. 3 -1
  4. D. 6 0

Correct answer: C. 3 -1

The test uses the old value and then decrements, so the body sees 2, 1 and 0 (s = 3). The failing test reads 0 and still decrements, leaving n at −1. '6' adds 3, 2, 1.

More C Programming questions

Exams that ask this topic