我疑惑这个代码片段:为什么在这个乘法中有一个整数溢出?
#include <climits>
#include <iostream>
int main(void) {
using namespace std;
cout << "long max " << LONG_MAX << endl;
long x = 2 * 1024 * 1024 * 1024;
cout << "2 * 1024 * 1024 * 1024 = " << x << endl;
return 0;
}
我期待理所应当的,而不是我得到。使用无符号似乎没有帮助。是什么赋予了?
long max 9223372036854775807
2 * 1024 * 1024 * 1024 = -2147483648
如果你输入'1024',它已经有一个数据类型,这个语言是整数。这就是为什么如果你想要长数据类型的话你需要指定'1024L'。 – eis 2013-03-26 22:55:05