ProgrammingVery hard

Which condition makes a loop with an int index i and a size_t bound n safe from an infinite loop when n is 0?

  1. A. The loop wraps i to SIZE_MAX, so it runs forever
  2. B. The loop is undefined because size_t is unsigned
  3. C. The loop condition i < n is false from the start when n is 0, so the body never runs
  4. D. The compiler removes the loop, which makes the output undefined

Correct answer: C. The loop condition i < n is false from the start when n is 0, so the body never runs

A comparison with an unsigned zero bound fails at once. The mixed signed and unsigned comparison is worth checking, but the result here is well defined.

More C Programming questions

Exams that ask this topic