No, it doesn't.
The requirement for a 2's-complement representation for values within the range of the type does not imply anything about the behavior on overflow.
The types in <stdint.h>
are simply typedefs (aliases) for existing types. Adding a typedef doesn't change a type's behavior.
Section 6.5 paragraph 5 of the C standard (both C99 and C11) still applies:
If an exceptional condition occurs during the evaluation of an
expression (that is, if the result is not mathematically defined or
not in the range of representable values for its type), the behavior
is undefined.
This doesn't affect unsigned types because unsigned operations do not overflow; they're defined to yield the wrapped result, reduced modulo TYPE_MAX + 1. Except that unsigned types narrower than int
are promoted to (signed) int
, and can therefore run into the same problems. For example, this:
unsigned short x = USHRT_MAX;
unsigned short y = USHRT_MAX;
unsigned short z = x * y;
causes undefined behavior if short
is narrower than int
. (If short
and int
are 16 and 32 bits, respectively, then 65535 * 65535
yields 4294836225
, which exceeds INT_MAX
.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…