Comparison operation on unsigned and signed integers
Binary operations between different integral types are performed within a "common" type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8). In your case the "common" type is unsigned int
. This means that int
operand (your b
) will get converted to unsigned int
before the comparison, as well as for the purpose of performing subtraction.
When -1
is converted to unsigned int
the result is the maximal possible unsigned int
value (same as UINT_MAX
). Needless to say, it is going to be greater than your unsigned 1000
value, meaning that a > b
is indeed false and a
is indeed small compared to (unsigned) b
. The if
in your code should resolve to else
branch, which is what you observed in your experiment.
The same conversion rules apply to subtraction. Your a-b
is really interpreted as a - (unsigned) b
and the result has type unsigned int
. Such value cannot be printed with %d
format specifier, since %d
only works with signed values. Your attempt to print it with %d
results in undefined behavior, so the value that you see printed (even though it has a logical deterministic explanation in practice) is completely meaningless from the point of view of C language.
Edit: Actually, I could be wrong about the undefined behavior part. According to C language specification, the common part of the range of the corresponding signed and unsigned integer type shall have identical representation (implying, according to the footnote 31, "interchangeability as arguments to functions"). So, the result of a - b
expression is unsigned 1001
as described above, and unless I'm missing something, it is legal to print this specific unsigned value with %d
specifier, since it falls within the positive range of int
. Printing (unsigned) INT_MAX + 1
with %d
would be undefined, but 1001u
is fine.
On a typical implementation where int
is 32-bit, -1 when converted to an unsigned int
is 4,294,967,295 which is indeed ≥ 1000.
Even if you treat the subtraction in an unsigned
world, 1000 - (4,294,967,295) = -4,294,966,295 = 1,001
which is what you get.
That's why gcc
will spit a warning when you compare unsigned
with signed
. (If you don't see a warning, pass the -Wsign-compare
flag.)
#include<stdio.h>
int main()
{
int a = 1000;
signed int b = -1, c = -2;
printf("%d",(unsigned int)b);
printf("%d\n",(unsigned int)c);
printf("%d\n",(unsigned int)a);
if(1000>-1){
printf("\ntrue");
}
else
printf("\nfalse");
return 0;
}
For this you need to understand the precedence of operators
-
Relational Operators works left to right ... so when it comes
if(1000>-1)
then first of all it will change -1 to unsigned integer because int is by default treated as unsigned number and it range it greater than the signed number
-1 will change into the unsigned number ,it changes into a very big number