Integer Ranks
Integer rank is C's ordering of integer types by width class. Promotions and the usual arithmetic conversions consult rank to decide the type of an expression when the operands differ.
Detailed Explanation
Every integer type has a rank. Rank is not the same as sizeof. It is a partial order the standard defines so that conversions have a single answer. From lowest to highest among the standard signed types: _Bool, char / signed char / unsigned char (all three share one rank), short, int, long, long long. An unsigned type has the same rank as its signed partner. A fixed-width type such as int32_t has the rank of whatever standard type it aliases.
Two conversions use this order.
Integer promotions apply to every char, short, bit-field, and _Bool the moment it appears in an expression. If int can represent every value of the original type, the value becomes int. Otherwise it becomes unsigned int. That is why adding two unsigned char values is done in int on a 32-bit int machine, and why unsigned char a = 200, b = 200; int s = a + b; yields 400 rather than a wrap to 144.
Usual arithmetic conversions apply when a binary operator sees two different arithmetic types. Floating types win over integers. Among integers, the operand with the higher rank is the target, with a signedness twist: if the higher-rank type is unsigned, the other operand converts to it. If the higher-rank type is signed and can hold every value of the unsigned operand, the unsigned operand converts to signed. If it cannot, both convert to the unsigned version of the higher-rank type.
That last clause is the source of the famous -1L < 1u surprises. On LP64, 1u is unsigned int and -1L is long. long has higher rank and can hold every unsigned int value, so 1u becomes 1L and the comparison is true. On ILP32, long and int are the same width, long cannot hold every unsigned int, both become unsigned long, and -1L becomes a huge unsigned number, so the comparison is false. Same source, two platforms, two answers.
Rank also explains why a cast is sometimes the only honest fix. If you mean a signed comparison, convert both sides to a signed type that can hold them. If you mean modular unsigned arithmetic, convert both sides to unsigned. Do not let rank pick for you when the values can be negative.
Code Examples
#include <stdio.h>
int main(void)
{
unsigned char a = 200;
unsigned char b = 200;
/* Both promote to int before `+`. The sum is 400, not 144. */
printf("%d\n", a + b); /* prints: 400 */
printf("%u\n", (unsigned char)(a + b)); /* prints: 144, the wrap you asked for */
return 0;
}#include <stdio.h>
int main(void)
{
unsigned int u = 1u;
long sl = -1L;
/* Result depends on whether long can hold every unsigned int.
LP64 (Linux/macOS 64-bit): 1
ILP32 (many 32-bit ABIs): 0 */
printf("%d\n", sl < u);
/* Say what you mean. Both of these are stable across ABIs. */
printf("%d\n", sl < (long)u); /* prints: 1 */
printf("%d\n", (unsigned long)sl < (unsigned long)u); /* prints: 0 */
return 0;
}Common Gotchas
Rank is not `sizeof`
`int` and `long` can have the same size and still have different ranks. Conversion still follows rank, then asks whether the signed type can hold the unsigned range.
Mixing signed values with unsigned operands is a defect magnet
enable `-Wsign-compare` and `-Wconversion`. When a warning fires, pick a type on purpose instead of silencing it.
Promotions happen even when both operands already match
two `short` values add as `int`. That is usually what you want, and it is why `short` overflow in an expression is rarer than people expect.
Bit-fields promote too
a 3-bit field in a struct is not left as a tiny unsigned type in arithmetic; it becomes `int` or `unsigned int` first.