【发布时间】:2012-12-04 01:12:45
【问题描述】:
可能重复:
size of int, long, etc
Is `long` guaranteed to be at least 32 bits?
我想为我的计算机找出每种数据类型的最大值。代码是:
int main() {
using namespace std;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<long>::max() << endl;
cout << numeric_limits<long long>::max() << endl;
return 0;
}
哪个打印:
2147483647
2147483647
9223372036854775807
问题1:为什么int和long是一样的?
问题2:以上输出来自VS2010 on 64bit。我的 c++ 程序是否以 64 位运行?
【问题讨论】:
-
我相信除了
sizeof(long) >= sizeof(int)和sizeof(long long) >= sizeof(long)之外,对位宽没有任何保证。 -
是的,您的程序可能作为 64 位可执行文件运行,
int传统上仍然是 32 位。 -
出于向后兼容的目的,Microsoft 选择在 64 位程序中将 long 设置为与 int 相同的大小。我们不知道您的程序是否是 64 位的。对于 VS 编译的 32 位和 64 位程序,int、long 和 long long 的最大值将相同。
-
@chris: 还有保证
int代表±32,767,long代表±2,147,483,647,long long代表±9,223,372,036,854,775,807。 -
@chris:在“numeric_limits members”部分中,
min()是“等效于CHAR_MIN、SHRT_MIN、FLT_MIN、DBL_MIN等”。这些是通过参考 C 标准来定义的。
标签: c++