Possible Duplicate:
unsigned int and signed char comparison
int j = 10;
unsigned int i = 10;
if( j > -1 )
printf("1\n");
else
printf("2\n");
if( i > -1 )
printf("3\n");
else
printf("4\n");
The output is :
1
4
I've traced into the assembly and the comparation is similar:
cmp dword ptr [ebp-10h],0FFFFFFFFh
...
cmp dword ptr [ebp-14h],0FFFFFFFFh
But still don't understand why one is true and the other is false.
IMO the cpu have no idea whether dword ptr is signed or not.
So how does it work under the hood?
UPDATE
anyone can explain it in assembly level?
As the other answers say, -1 is being converted to unsigned int. Check the jump instructions that follow. One should be "ja" and the other "jg": the unsigned and signed jump instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With