Reputation: 79
int main()
{
char a = 'P';
char b = 0x80;
printf("a>b %s\n",a>b ? "true":"false");
return 0;
}
Why does it evaluates to true?
Upvotes: 0
Views: 109
Reputation: 224310
On your system, char
is signed. It is also eight bits, so 0x80
overflows what a signed 8-bit integer can represent. The resulting value is -128. Since P
is some positive value, it is greater than -128.
C permits the char
type to be signed or unsigned. This is a special (annoying) property, unlike other integer types such as int
. It is often advisable to explicitly declare character types with unsigned char
so that the behavior is more determined rather than implementation-dependent.
Upvotes: 10