ProgrammingHardProduct company OA

Assume 32-bit unsigned int. What is printed?

Assume 32-bit unsigned int. What is printed?
unsigned int n = 5;
int count = 0;
for (unsigned int i = n; i < n + 3; i -= 2)
    count++;
printf("%d", count);
  1. A. 2
  2. B. Nothing: it loops forever
  3. C. 4
  4. D. 3

Correct answer: D. 3

i takes 5, 3, 1 (all below 8). Then 1 − 2 wraps to 4294967295, which is not below 8, so the loop stops after 3 passes. Unsigned wrap-around is well defined; the infinite-loop answer imagines i going negative.

More C Programming questions

Exams that ask this topic