Given that code :
int main(void) {
long x = -1;
if (x < sizeof(x)) {
printf("OK\n");
}
else {
printf("Not Ok\n");
}
return 0;
}
Prints:
Not Ok
I have read about usual arithmetic conversions, and from what i understood that if we have a comparison between two operands with different data types, the compiler invokes a data coercion on the operand which is narrower than the other operand, which will convert the the sizeof(x)
into unsigned int
to match the other operand, any thoughts?
Aside question:
I tried to do casting size(x)
as int
in the comparison expression as:
if(x < (int) size(x))
It prints Ok
, which I find very strange!