C/C++ Arena

Step 2 of 7

Unsigned wraparound

Unsigned integers can't be negative. Their arithmetic is defined to wrap around modulo 2ᴺ: go below 0 and you land at the top.

unsigned int x = 0;
x = x - 1;               // 4294967295, not -1

This is well-defined (unlike signed overflow, next step), and it's useful for hashing and bit manipulation. It's also a classic trap in countdown loops:

for (size_t i = n - 1; i >= 0; i--)   // i >= 0 is ALWAYS true: infinite loop

The idiomatic safe countdown tests before decrementing:

for (size_t i = n; i-- > 0; )         // visits n-1, n-2, ..., 0

Your turn: read a count n and then n integers into an array (at most 100), and print them in reverse order on one line, separated by spaces. Use a size_t index for the reverse loop.

Previous: Sizes and limits Next: Signed overflow is undefined