0
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?

P0W
  • 46,614
  • 9
  • 72
  • 119
  • The ASCII value of `P` happens to be (decimal) 80. Did you confuse decimal `80` vs. hexadecimal `0x80`? – Keith Thompson Sep 06 '13 at 18:17
  • read [What is -(-128) for signed single byte char in C?](http://stackoverflow.com/questions/17469804/what-is-128-for-signed-single-byte-char-in-c/17469941#17469941) – Grijesh Chauhan Sep 07 '13 at 07:58

1 Answers1

10

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312