C: A Reference Manual, 5th Edition, page 129:
Whether an ingeger is signed or unsigned affects the operations performed on it. All
arithmetic operations on unsigned integers behave according to the rules of modular (con-
gruence) arithmetic modulo 2
n. So, for example, addint 1 to the largest value of an unsigned
type is guarinteed to produce 0. The behavior of overflow is well defined.
Expressions that mix signed and unsigned integers are forced to use unsigned opera-
tions. Section 6.4.3 discusses the conversions performed, and Chapter 7 discusses the
effect of each operator when its arguments are unsigned.
Example
These conversions can be surprising. For example, because unsigned integers are always non-
negative, you would expect that the following test would always be true:
Code:
unsigned int u;
...
if( u > -1 ) ...
However, it is always false! The (signed) -1 is converted to an unsigned integer before the
comparison, yielding the largest unsigned integer, and the value of
u cannot be greater than
that integer.
The origional definition of C provided only a single unsigned type, unsigned. Most
non-Standard C implementations now provide the full range of unsigned types.
...