ProgrammingVery hard

unsigned int i; for (i = 10; i >= 0; i--) { } What is the behaviour of this loop?

  1. A. It runs 11 times and stops
  2. B. It is undefined behaviour because i wraps
  3. C. It never terminates, because an unsigned value is never negative, so i >= 0 is always true
  4. D. It runs 10 times and stops

Correct answer: C. It never terminates, because an unsigned value is never negative, so i >= 0 is always true

i >= 0 is always true for an unsigned type, and i wraps from 0 to UINT_MAX, so the loop never ends. The standard defines the wrap, so the loop is an infinite loop, not undefined.

More C Programming questions

Exams that ask this topic