c – 大的负整数文字
在Visual Studio 2010下面的程序
#include <iostream> using std::cout; int main() { cout << -2147483646 << 'n'; cout << -2147483647 << 'n'; cout << -2147483648 << 'n'; // numeric_limits<int>::min() cout << -2147483649 << 'n'; cout << -2147483650 << 'n'; cout << "..." << 'n'; cout << -4294967293 << 'n'; cout << -4294967294 << 'n'; cout << -4294967295 << 'n'; // -numeric_limits<unsigned int>::max() cout << -4294967296 << 'n'; cout << -4294967297 << 'n'; } 生成以下输出 -2147483646 -2147483647 2147483648 2147483647 2147483646 ... 3 2 1 -4294967296 -4294967297 到底是怎么回事? 这是标准行为还是Visual Studio错误? 编辑:正如几个人所指出的那样,没有一个负整数文字的东西.有关详细信息,请参阅Keith Thompson的以下优秀答案. 解决方法
-2147483648,例如,不是整数文字;它是一个由一元运算符组成的表达式,应用于文字2147483648.
在新的C 2011标准之前,C不要求存在大于32位的任何类型(C 2011增加了长的一段时间),所以文字2147483648是不可移植的. 十进制整数文字是其值适合的以下类型中的第一个: int long int long long int (new in C++ 2011) 请注意,它在标准C中从不是无符号类型.在1998年和2003年的C标准版本(没有long long int)中,一个十进制整数字面值太大而不能适应长整型,导致未定义的行为.在C 2011中,如果一个十进制整数字符串不适用于long long int,那么该程序是“不正确的”. 但是,gcc(至少在4.6.1版本中是最新的)没有实现C 2011语义.不符合32位长的文字2147483648至少在我的32位系统上被视为无符号长. (对于C 98或C 2003而言,行为是未定义的,所以编译器可以做任何它喜欢的事情.) 所以给出一个典型的32位2s补码int类型,这样: cout << -2147483647 << 'n'; 取int值2147483647,否定它,并打印结果,这与您期望的数学结果相匹配.但是这个: cout << -2147483648 << 'n'; (当使用gcc 4.6.1编译时)使用long或unsigned long值2147483648,将其取消为unsigned int,生成2147483648,并打印出来. 如其他人所提到的,您可以使用后缀来强制特定类型. 这是一个小程序,您可以使用它来显示编译器如何对待文字: #include <iostream> #include <climits> const char *type_of(int) { return "int"; } const char *type_of(unsigned int) { return "unsigned int"; } const char *type_of(long) { return "long"; } const char *type_of(unsigned long) { return "unsigned long"; } const char *type_of(long long) { return "long long"; } const char *type_of(unsigned long long) { return "unsigned long long"; } int main() { std::cout << "int: " << INT_MIN << " .. " << INT_MAX << "n"; std::cout << "long: " << LONG_MIN << " .. " << LONG_MAX << "n"; std::cout << "long long: " << LLONG_MIN << " .. " << LLONG_MAX << "n"; std::cout << "2147483647 is of type " << type_of(2147483647) << "n"; std::cout << "2147483648 is of type " << type_of(2147483648) << "n"; std::cout << "-2147483647 is of type " << type_of(-2147483647) << "n"; std::cout << "-2147483648 is of type " << type_of(-2147483648) << "n"; } 当我编译它,我会得到一些警告: lits.cpp:18:5: warning: this decimal constant is unsigned only in ISO C90 lits.cpp:20:5: warning: this decimal constant is unsigned only in ISO C90 和以下输出,即使使用gcc -std = c 0x: int: -2147483648 .. 2147483647 long: -2147483648 .. 2147483647 long long: -9223372036854775808 .. 9223372036854775807 2147483647 is of type int 2147483648 is of type unsigned long -2147483647 is of type int -2147483648 is of type unsigned long 我得到与VS2010相同的输出,至少使用默认设置. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |