The "C ++ Primer (fifth version)" says:
The type of integer literal depends on its meaning and designation. By default, decimal literals are signed, while octal and hexadecimal literals can be either signed or unsigned types. The decimal literal is of the smallest type int, long or long long, in which the literal value matches. Octal and hexadecimal literals are of the smallest type int, unsigned int, long, unsigned long, long long or unsigned long long in which the literal value matches.
Running the following code in Visual C ++ 2015:
#include <iostream> using namespace std; void verify_type(int a) { cout << "int" << endl; } void verify_type(long a) { cout << "long" << endl; } void verify_type(long long a) { cout << "long long" << endl; } void verify_type(unsigned int a) { cout << "unsigned int" << endl; } void verify_type(unsigned long a) { cout << "unsigned long" << endl; } void verify_type(unsigned long long a) { cout << "unsigned long long" << endl; } int main() { cout << "The value of INT_MAX is " << INT_MAX << endl; cout << "The value of INT_MIN is " << INT_MIN << endl; verify_type(2147483648); verify_type(0x80000000U); verify_type(0x80000000); system("pause"); return 0; }
I get this:
The value of INT_MAX is 2147483647 The value of INT_MIN is -2147483648 unsigned long unsigned int unsigned int
I would expect verify_type(2147483648) be long long . Why am I getting unsigned long ?
source share