#include <stdio.h>
#include <stdint.h>
int main()
{
uint16_t peter = 8;
uint32_t peter2 = 8;
if(peter > -1)
{
printf("Peter true\n"); //expected
}
if (peter2 > -1)
{
printf("Peter 2 true\n"); //wtf, why not
}
return 0;
}
Why does the first statement enter the clause and the second does not (on a 32bit architecture)?
The explanation for the difference between
peter > -1andpeter2 > -1are the notions of “integer promotion”, and also “usual arithmetic conversions”.“Integer promotion” means that any integer type
tnarrower thanintis “promoted” to eitherint(ifintcan contain all the values oft) orunsigned intotherwise. In your example and with your compiler, promotion is applied topeter, which is converted tointbefore being compared to-1(which also has typeint).Then, the “usual arithmetic conversions” decide what happen when an arithmetic binary operation, here
>, is applied to operands of different types. Between an unsigned and a signed integer type of the same width, these rules say that the signed operand is converted to unsigned before the operation takes place.-1has typeint. When it is compared topeter2, the usual arithmetic conversions mean-1is converted touint32_t, becoming a large value (greater than8).