Signed integers (we'll use 16 bit) range from -32768 to 32767 (0x8000 to 0x7FFF) while unsigned integers range from 0 to 65535 (0x0000 to 0xFFFF). So unsigned integers cannot have negative values, which is why your loop never terminates. However, you have told the print program to format the output as if it were signed (%d
) so that you see the numbers formatted as negative values in your output. At some level, everything in a computer is just a string of bits that needs interpretation and your code example uses two different interpretations of the same bit patterns ... your unsigned int.