The word unsigned denotes exactly what it means. That is in this declaration
unsigned int a = -1;
the variable a
has unsigned integer type that means the range of values that can be stored in the variable is [0, UINT_MAX]
.
The initializer -1
is implicitly converted to the type of the initialized object by propagating the sign bit (if required) for the two's complement internal representation of integers. As a result as all bits are set the value of the expression (unsigned int )-1
is the maximum value for the type unsigned int.
In conditions like the condition in this if statement
if (a<b)
the common type of operands is deduced. Due to the usual arithmetic conversions
as the variables a and b have the same conversion rank then the object b of the type int
is converted to the type unsigned int
.
And in fact there are compared ( unsigned int )-1
with ( unsigned int )-1
that are equal each other expressions.
From the C Standard (6.3.1.8 Usual arithmetic conversions)
Otherwise, if the operand that has unsigned integer type has rank
greater or equal to the rank of the type of the other operand, then
the operand with signed integer type is converted to the type of the
operand with unsigned integer type.
The ranks of the types unsigned int
and int
are the same.