Machine-translated by Google. Questions are authored in English.
Top

Discussion

#include <stdio.h>
 
int main()
{
    unsigned int i = 65000;
    while (i++ != 0);
    printf("%d", i);
    return 0;
}

 

  • A.Infinite Loop
  • B.1
  • C.Run Time Error

Answer: B

Correct answer: 1

i++ yields the old value and then increments, so the loop keeps going while the old value is non-zero. Starting at 65000 the variable climbs to the maximum for an unsigned int and then wraps round to 0 - wrapping is defined behaviour for unsigned types. On that pass the test sees 0 and stops, but i has already been incremented once more, to 1.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.