#include<stdio.h>
int main()
{
unsigned int a=6;
int b=-20;
(a+b>6)?puts(">6"):puts("<=6");
return 0;
}
The above code outputs >6. But I got a doubt. b=-20 will hold a negative value (-18)
after doing the 2’s complement as it’s a signed integer. So it should output <=6 but its
giving an output as >6.
From the C99 standard, section 6.3.1.8 (“Usual arithmetic conversions”):
Basically, in the case of your example code,
unsigned intandinthave the same rank, so it’s equivalent to:To fix it, you need to explicitly cast in the other direction, i.e.: