int 0x80



gorlist@int0x80:~$ cat news.txt

C quirks
Consider the following code snippet:

#include <stdio.h>

int main(void)
{
int a = 2;
unsigned int b = 2;
int c = -1;

if (a + b < c)
puts("weird.");

return 0;
}

Why does that happen? Why does it print the message?
Variable 'c' gets promoted to unsigned int.

The c99 rationale describes it best in "6.3.1.1 Booleans, characters, and integers".
Excerpt:
Exactly the same ambiguity arises whenever an unsigned int confronts a signed int
across an operator, and the signed int has a negative value. Neither scheme does any better,
5 or any worse, in resolving the ambiguity of this confrontation. Suddenly, the negative signed
int becomes a very large unsigned int, which may be surprising, or it may be exactly what
is desired by a knowledgeable programmer. Of course, all of these ambiguities can be avoided
by a judicious use of casts.
0 Comments
Posted on 05 May 2008 by gorlist


Comments
name:
email: * (will not be published)
website:
comment:
Linux creator's first name