C Reference ยท Types and Literals
C unsigned integer types and wraparound
Reference for C unsigned integer types: modular wraparound, usual arithmetic conversions with signed values, comparison traps, loops and printf specifiers.
An unsigned integer type holds only zero and positive values. In exchange for giving up negative numbers, it gains one extra bit of range and, more importantly, defined overflow: arithmetic on unsigned types is done modulo 2^N, where N is the width of the type. Going past the maximum wraps around to zero, and going below zero wraps to the maximum.
Every standard integer type has an unsigned form: unsigned char, unsigned short, unsigned int (or just unsigned), unsigned long, unsigned long long. The types size_t and uint32_t are unsigned, which is why the rules here matter in daily C.
unsigned int u = 0; u = u - 1; /* UINT_MAX, not -1 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
unsigned a; /* unsigned int */ unsigned char b; unsigned short c; unsigned long d; unsigned long long e;
| Type | Typical maximum | printf |
Literal suffix |
|---|---|---|---|
unsigned char |
255 (UCHAR_MAX) |
%hhu |
none |
unsigned short |
65535 (USHRT_MAX) |
%hu |
none |
unsigned int |
4294967295 (UINT_MAX) |
%u, %x, %o |
U |
unsigned long |
2^64 - 1 on Linux, 2^32 - 1 on Windows (ULONG_MAX) |
%lu |
UL |
unsigned long long |
2^64 - 1 (ULLONG_MAX) |
%llu |
ULL |
Usual arithmetic conversions: when a signed and an unsigned operand of the same rank meet, the signed one is converted to unsigned. Operands narrower than int are first promoted to (signed) int, so unsigned char and unsigned short arithmetic does not wrap until the result is stored back.
Errors / undefined behavior: unsigned overflow is not undefined; it wraps. But shifting by a count at least as large as the width, or dividing by zero, is undefined. Converting a negative signed value to unsigned is well defined (it adds 2^N); converting an out-of-range value to a signed type is implementation-defined.
Usage notes
- Comparison trap.
-1 < 1uis false, because-1becomesUINT_MAX. GCC warns with-Wsign-compare, which is part of-Wextra. - Subtraction trap.
a - bwitha < bwraps to a huge number. Compare first, or compute in a signed type. - Loop trap.
for (unsigned i = n - 1; i >= 0; i--)never ends becausei >= 0is always true. Use thei-- > 0idiom shown above. - Bit manipulation is clearer on unsigned values. Right-shifting a negative signed value is implementation-defined; right-shifting unsigned fills with zeros.
- Do not use unsigned just to mean "never negative". Mixing it with signed counters invites the traps above. Reserve unsigned types for bit masks, hashes, modular arithmetic and sizes (
size_t). - Hexadecimal and octal literals may be signed or unsigned depending on the value; add
Uto be explicit. - Printing:
%uwith a signedintargument is technically a mismatch for negative values, though it usually prints the wrapped number. - Safe overflow checks are available in C23 as
ckd_addand friends.