在“C ++ Primer(第五版)”中,它说:
整数文字的类型取决于其值和符号。 默认情况下,十进制文字是有符号的,而八进制和十六进制文字可以是有符号或无符号类型。 十进制文字具有文字值适合的最小类型int,long或long long。 八进制和十六进制文字具有最小类型的int,unsigned int,long,unsigned long,long long或unsigned long long 其中文字的价值适合。
在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;
}
我明白了:
The value of INT_MAX is 2147483647
The value of INT_MIN is -2147483648
unsigned long
unsigned int
unsigned int
我希望verify_type(2147483648)
为long long
。为什么我会unsigned long
?