comparing signed and unsigned values

+1 vote
asked Apr 11, 2018 by anonymous

#include <stdio.h>
unsigned int a=1;
int b=-2;
int main()
{
if(a > b)
{
printf("Ok");
}
else
{
printf("Not Ok");
}

printf(" a=%x , b=%x",a,b);
return 0;
}

// gives output Not ok , due to binary comparison 


// but when int is changed to char comparison happen correctly , why ?

#include <stdio.h>
unsigned char a=1;
char b=-2;
int main()
{
if(a > b)
{
printf("Ok");
}
else
{
printf("Not Ok");
}

printf(" a=%x , b=%x",a,b);
return 0;
}
// prints Ok 

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...