ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
unsigned int i;
int count = 0;
for (i = 3; i >= 0; i--) {
    if (++count == 5) break;
}
printf("%d %u", count, i);      /* 32-bit unsigned int */
  1. A. 4 0
  2. B. 5 0
  3. C. 5 4294967295
  4. D. 4 4294967295

Correct answer: C. 5 4294967295

i >= 0 is always true for an unsigned value, so without the break this would loop forever. Counts 1–4 happen at i = 3, 2, 1, 0; then i-- wraps to 4294967295 (UINT_MAX) and count reaches 5 there.

More C Programming questions

Exams that ask this topic