I have been reading about comparing signed and unsigned integers on https://www.securecoding.cert.org/confluence/display/seccode/INT02-C.+Understand+integer+conversion+rules
. This page gives a simple example:
int si = -1;
unsigned int ui = 1;
printf("%d\n", si < ui);
The output is 0, not 1 as might be initially believed, as the int s1 is converted to an unsigned int with value UINT_MAX.
This is something to do with conversion rules and integer ranking; I roughly understand the conversion rules ("Usual Arithmetic Conversions" section on the link) - and believe that rule #3 is being used:
If the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type is converted to the type of the operand with unsigned integer type.
...but I don't understand the ranking: clearly for the conversion above to take place the unsigned value has been given a greater rank than the signed value but where is it specified that this will happen??
Can someone please clarify ?
(Sorry if I have missed it from the above link but I have spent a long time looking at this which might be to blame:))