GATE Computer ScienceHardGATE CS (PYQ pattern)

unsigned int i, s = 0; for (i = 5; i-- > 0; ) s += i; printf("%u %u", s, i); What is printed?

  1. A. 10 0
  2. B. 15 4294967295
  3. C. 10 4294967295
  4. D. 10 4294967294

Correct answer: C. 10 4294967295

The condition i-- > 0 tests the old value and then decrements, so the body sees i = 4, 3, 2, 1, 0 (sum 10). After the last test (0 > 0 false) i is decremented once more to 0 - 1 = 4294967295 (unsigned wrap).

More Programming and Data Structures (GATE) questions

Exams that ask this topic