15

What is the defined behavior in C for UINT_MAX + 1u? How safe is to assume it is zero?

2
  • 3
    @JoshPetitt: That's still zero by the way. UINT_MAX + 1u is evaluated as an unsigned integer, resulting in 0, which is then stored in a long long. ;-)
    – netcoder
    Feb 15, 2013 at 16:49
  • @netcoder, on my Windows 64-bit machine you are indeed correct. My internal compiler is flawed. :-) Feb 15, 2013 at 16:58

4 Answers 4

28

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.

2
  • 11
    The standard mandates that UINT_MAX (and the maximal values of other unsigned integer types) is 2^N - 1, where N is the width of the type, so 10 is a bad example (but that doesn't stop me from upvoting). Feb 15, 2013 at 16:56
  • Furthermore, (unsigned) -1 == UINT_MAX; the latter can be used in preprocessor conditions, while (unsigned) -1 can only be evaluated by the compiler.
    – Loic
    Apr 26, 2015 at 19:50
12

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             0
LONG_MAX+1             undefined
INT_MAX+1              undefined
SHRT_MAX+1             SHRT_MAX+1 if INT_MAX>SHRT_MAX, otherwise undefined
char c = CHAR_MAX; c++ varies
-INT_MIN               undefined
(char)INT_MAX          commonly -1
1<<-1                  undefined
1<<0                   1
1<<31                  commonly INT_MIN in ANSI C and C++98; undefined in C99 and C++11
1<<32                  undefined
1/0                    undefined
INT_MIN%-1             undefined in C11, otherwise undefined in practice
5

It's safe. The C standard guarantees that unsigned integer overflow wrap-around results in zero.

3
  • 2
    And C strictly speaking, unsigned integers never overflow only signed integers overflow.
    – ouah
    Feb 15, 2013 at 16:41
  • @ouah In this case, what should I say they do?
    – user529758
    Feb 15, 2013 at 16:43
  • 1
    @H2CO3: you could say they wrap around. Feb 15, 2013 at 17:03
2

Should be safe:

Wiki on unsigned overflow

Note the unsigned int overflow is well defined.

Also, here's a whole question on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.