UINT_MAX + 1 equals what?
From the standard (C11, 6.2.5/9, emphasis mine):
[...] A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
If UINT_MAX
is 10
:
(10 + 1) % (10 + 1) == 0
So, yes, it's safe to assume it's zero.
It's worth emphasizing that while unsigned behavior is well-defined, signed integer overflow isn't:
In the C programming language, signed integer overflow causes undefined behavior, while unsigned integer overflow causes the number to be reduced modulo a power of two
A very good paper on the subject:
EXAMPLES OF C/C++ INTEGER OPERATIONS AND THEIR RESULTS
Expression Result---------- ------UINT_MAX+1 0LONG_MAX+1 undefinedINT_MAX+1 undefinedSHRT_MAX+1 SHRT_MAX+1 if INT_MAX>SHRT_MAX, otherwise undefinedchar c = CHAR_MAX; c++ varies-INT_MIN undefined(char)INT_MAX commonly -11<<-1 undefined1<<0 11<<31 commonly INT_MIN in ANSI C and C++98; undefined in C99 and C++111<<32 undefined1/0 undefinedINT_MIN%-1 undefined in C11, otherwise undefined in practice
It's safe. The C standard guarantees that unsigned integer overflow wrap-around results in zero.