Signed vs Unsigned
A signed integer can be negative. An unsigned integer cannot: arithmetic on it wraps modulo two to the power of its width. Mixing the two in one expression is one of C's most reliable sources of silent bugs.
Detailed Explanation
Signed types — signed char, short, int, long, long long — represent negative and non-negative values. Since C23 the standard requires two's complement, which is what every modern CPU already used. Overflow on a signed type is undefined behavior. The compiler may wrap, trap, or delete the overflow check it decided was impossible. You cannot portably detect signed overflow after it happens; you have to test before the operation.
Unsigned types — unsigned char, unsigned short, unsigned, unsigned long, unsigned long long — represent values from 0 to 2^n - 1. Arithmetic is defined to wrap. unsigned x = 0; x--; is well-defined and yields the maximum value of the type. That wrap is a feature when you want modular arithmetic, and a defect when you meant "one less than zero is an error."
Pick unsigned when the value is a size, an index, a bit mask, or a quantity that cannot be negative by construction. size_t is unsigned for this reason. Pick signed when the value is a difference, a temperature, an error-or-count return, or anything that will be subtracted and compared. ptrdiff_t is signed because a pointer difference can be negative.
The dangerous combination is a signed operand next to an unsigned one. Usual arithmetic conversions will often turn the signed value into unsigned before the operator runs. A loop written for (int i = n - 1; i >= 0; i--) is fine. A loop written for (size_t i = n - 1; i >= 0; i--) never ends, because i >= 0 is always true for an unsigned i. The same trap appears as if (len - 1 < 0) when len is size_t: unsigned subtraction cannot produce a negative, so the test is meaningless.
Conversion from unsigned to a signed type that cannot hold the value is implementation-defined. Conversion the other way is well-defined wrap. Neither one is a good surprise in an API boundary. Cast explicitly and comment why, or change the types so the cast is unnecessary.
Code Examples
#include <stdio.h>
#include <stddef.h>
/* Unsigned countdown. Stopping at 0 needs a different test than `i >= 0`. */
static void print_rev(const int *a, size_t n)
{
for (size_t i = n; i-- > 0; ) { /* test, then decrement; stops after i == 0 */
printf("%d ", a[i]);
}
printf("\n");
}
int main(void)
{
int values[] = { 1, 2, 3 };
print_rev(values, 3); /* prints: 3 2 1 */
return 0;
}#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned u = 0;
u--;
printf("%u\n", u); /* prints: 4294967295 on 32-bit unsigned */
int a = -1;
unsigned b = 1u;
printf("%d\n", a < b); /* prints: 0, because a becomes UINT_MAX */
/* Detect unsigned wrap before it happens. */
unsigned x = UINT_MAX - 2;
unsigned add = 5;
if (x > UINT_MAX - add) {
printf("would wrap\n"); /* prints: would wrap */
}
return 0;
}Common Gotchas
`size_t i = n; i >= 0` is always true
an unsigned counter cannot go negative. Walk downward with `i-- > 0` or iterate upward.
Signed overflow is undefined behavior
`int x = INT_MAX; x++;` may be deleted by the optimizer. Use a wider type, a pre-check, or unsigned arithmetic if wrap is the intended result.
A negative signed value converted to unsigned becomes a huge number
that conversion is well-defined and almost never what a comparison wanted.
`unsigned` is a good bit-mask type and a poor "count that might fail" type
APIs that return `-1` on error cannot use `size_t` for the success path without a side channel.