Why is an unsigned int 1 lower than a char y -1?
If char
is equivalent to signed char
:
char
is promoted toint
(Integer Promotions, ISO C99 §6.3.1.1 ¶2)- Since
int
andunsigned int
have the same rank,int
is converted tounsigned int
(Arithmetic Conversions, ISO C99 §6.3.1.8)
If char
is equivalent to unsigned char
:
char
may be promoted to eitherint
orunsigned int
:- If
int
can represent allunsigned char
values (typically becausesizeof(int) > sizeof(char)
),char
is converted toint
. - Otherwise (typically because
sizeof(char)==sizeof(int)
),char
is converted tounsigned
.
- If
- Now we have one operand that is either
int
orunsigned int
, and another that isunsigned int
. The first operand is converted tounsigned int
.
Integer promotions:
An expression of a type of lower rank that int
is converted to int
if int
can hold all of the values of the original type, to unsigned int
otherwise.
Arithmetic conversions: Try to convert to the larger type. When there is conflict between signed and unsigned, if the larger (including the case where the two types have the same rank) type is unsigned, go with unsigned. Otherwise, go with signed only in the case it can represent all the values of both types.
Conversions to integer types(ISO C99 §6.3.1.3):
Conversion of an out-of-range value to an unsigned integer type is done via wrap-around (modular arithmetic).
Conversion of an out-of-range value to a signed integer type is implementation defined, and can raise a signal (such as SIGFPE).
When using signed and unsigned in single operation the signed got promoted to unsigned by C's automatic type conversion. If the bit patter of -1
is considered an unsigned number then it is a very very high value. So x > y
is false.