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)

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 " Booleans, characters, and integers".
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.
Posted on 05 May 2008 by gorlist

email: * (will not be published)
First name of C++ creator

Copyright © gorlist 2007-2017