Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about unsigned int in c [duplicate]

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?

like image 466
kern Avatar asked Mar 19 '26 05:03

kern


1 Answers

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.

like image 193
Neil G Avatar answered Mar 21 '26 18:03

Neil G