ProgrammingModerateAccenture · Cognizant pattern

What happens?

What happens?
for (unsigned int i = 3; i >= 0; i--)
    printf("%u ", i);
  1. A. Prints 3 2 1 0
  2. B. Compilation error
  3. C. Prints 3 2 1
  4. D. Infinite loop: 3 2 1 0 4294967295 ... (32-bit unsigned)

Correct answer: D. Infinite loop: 3 2 1 0 4294967295 ... (32-bit unsigned)

An unsigned value is never negative, so i >= 0 is always true. After 0, i-- wraps around to UINT_MAX and the loop never ends. Use a signed counter or test i-- > 0.

More C Programming questions

Exams that ask this topic