为什么这个程序的输出是-2147483648?
#include <iostream>
using namespace std;
int main() {
long long a=-2147483648;
a=a*-1;
cout<<a;
return 0;
}
它应该是 2147483648 因为它在 long long
范围内。为什么标志没有变化?我什至尝试过 abs()
函数,但结果是一样的。
另外更令人惊讶的是,这个程序输出了2147483648:
#include <iostream>
using namespace std;
int main() {
long long a=-2147483648;
a=a*-1;
a=a*-1;
cout<<a;
return 0;
}
第二次,乘以 -1 成功了。如果重要的话,我使用的是 C++ 4.8.1。
最佳答案
启用-Wall
,您会看到答案。
foo.C:5: warning: this decimal constant is unsigned only in ISO C90
使用 -2147483648LL
代替常量。
关于c++ - 大负数乘以 -1 给出输出负数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20334607/