The type on the left of the initialization does not matter. The expression -2147483648
is interpreted by itself, independently.
Literal 2147483648
has no suffixes, which means that the compiler will first make an attempt to interpret it as an int
value. On your platform int
is apparently a 32-bit type. Value 2147483648
falls outside the range of signed 32-bit integer type. The compiler is required to use wider signed integer type to represent the value, if one is available (in the int
, long int
, long long int
sequence, with the last one being formally available from C++11 on). But if no sufficiently wide signed integer type is available, the behavior is undefined
In MSVC historically long int
has the same width as int
, albeit 64-bit long long int
is also supported in later versions of MSVC. However, even in VS2013 (which supports long long int
) I get
warning C4146: unary minus operator applied to unsigned type, result still unsigned
in response to your initialization. This means that MSVC still sticks to archaic C89/90 rules of integer literal interpretation (where types were chosen from int
, long int
, unsigned long int
sequence).
Note that MSVC is not officially a C++11 compiler. So formally it does not have to try long long int
. Such type does not formally exist in pre-C++11 language. From that point of view, MSVC has no sufficiently large signed integer type to use in this case and the behavior is undefined. Within the freedom provided by undefined behavior, the use of C89/90 rules for integer literal interpretation is perfectly justifiable.
You might also take a look at this (-2147483648> 0) returns true in C++?