(-2147483648> 0) returns true in C++?
The compiler (VC2012) promote to the "minimum" integers that can hold the values. In the first case, signed int
(and long int
) cannot (before the sign is applied), but unsigned int
can: 2147483648
has unsigned int
???? type.
In the second you force int
from the unsigned
.
const bool i= (-2147483648 > 0) ; // --> true
warning C4146: unary minus operator applied to unsigned type, result still unsigned
Here are related "curiosities":
const bool b= (-2147483647 > 0) ; // false
const bool i= (-2147483648 > 0) ; // true : result still unsigned
const bool c= ( INT_MIN-1 > 0) ; // true :'-' int constant overflow
const bool f= ( 2147483647 > 0) ; // true
const bool g= ( 2147483648 > 0) ; // true
const bool d= ( INT_MAX+1 > 0) ; // false:'+' int constant overflow
const bool j= ( int(-2147483648)> 0) ; // false :
const bool h= ( int(2147483648) > 0) ; // false
const bool m= (-2147483648L > 0) ; // true
const bool o= (-2147483648LL > 0) ; // false
C++11 standard:
2.14.2 Integer literals [lex.icon]
…
An integer literal is a sequence of digits that has no period or exponent part. An integer literal may have a prefix that specifies its base and a suffix that specifies its type.
…
The type of an integer literal is the first of the corresponding list in which its value can be represented.
If an integer literal cannot be represented by any type in its list and an extended integer type (3.9.1) can represent its value, it may have that extended integer type. If all of the types in the list for the literal are signed, the extended integer type shall be signed. If all of the types in the list for the literal are unsigned, the extended integer type shall be unsigned. If the list contains both signed and unsigned types, the extended integer type may be signed or unsigned. A program is ill-formed if one of its translation units contains an integer literal that cannot be represented by any of the allowed types.
And these are the promotions rules for integers in the standard.
4.5 Integral promotions [conv.prom]
A prvalue of an integer type other than
bool
,char16_t
,char32_t
, orwchar_t
whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of typeint
ifint
can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of typeunsigned int
.
-2147483648
is not a "number". C++ language does not support negative literal values.
-2147483648
is actually an expression: a positive literal value 2147483648
with unary -
operator in front of it. Value 2147483648
is apparently too large for the positive side of int
range on your platform. If type long int
had greater range on your platform, the compiler would have to automatically assume that 2147483648
has long int
type. (In C++11 the compiler would also have to consider long long int
type.) This would make the compiler to evaluate -2147483648
in the domain of larger type and the result would be negative, as one would expect.
However, apparently in your case the range of long int
is the same as range of int
, and in general there's no integer type with greater range than int
on your platform. This formally means that positive constant 2147483648
overflows all available signed integer types, which in turn means that the behavior of your program is undefined. (It is a bit strange that the language specification opts for undefined behavior in such cases, instead of requiring a diagnostic message, but that's the way it is.)
In practice, taking into account that the behavior is undefined, 2147483648
might get interpreted as some implementation-dependent negative value which happens to turn positive after having unary -
applied to it. Alternatively, some implementations might decide to attempt using unsigned types to represent the value (for example, in C89/90 compilers were required to use unsigned long int
, but not in C99 or C++). Implementations are allowed to do anything, since the behavior is undefined anyway.
As a side note, this is the reason why constants like INT_MIN
are typically defined as
#define INT_MIN (-2147483647 - 1)
instead of the seemingly more straightforward
#define INT_MIN -2147483648
The latter would not work as intended.