使用c++和VS2008管理整数时的错误
Bug when managing whole numbers using C++ and VS2008?
我正在调试一个c++程序,我发现,由于某种原因,在我的visual studio 2008版本下,一些数字似乎不能正确存储。例如,数字-3254535440似乎被存储为1040431856。这些数字足够小,可以存储在一个很长的long中,所以应该没有问题。如果运行以下代码,将显示两条错误消息。我在两台不同的机器上试过。任何想法?谢谢!
if (-3254535440 == 1040431856)
printf("ERRORn");
long long j = -3254535440;
if (j == 1040431856)
printf("ERROR2n");
在Microsoft Visual Studio 2008 Version 9.0.30729.1 SP下测试
文字仍然是int
s。你需要把它们变成一个更大的字体:
long long j = -3254535440LL;
这并不适合32位类型(似乎您的int
是),但一旦表示为long long
,它将保持其值
使用更高的警告级别编译,您将得到一个警告:
#include <stdio.h>
int main()
{
long long j = -3254535440;
printf("%lldn",j);
}
编译器:
c:>cl /nologo /W4 test.cpp
test.cpp
test.cpp(5) : warning C4146: unary minus operator applied to unsigned type, result still unsigned
c:>test
1040431856
如前所述,声明一个long long字面量:
#include <stdio.h>
int main()
{
long long j = -3254535440LL;
printf("%lldn",j);
}
结果:c:>cl /nologo /W4 test.cpp
test.cpp
c:>test
-3254535440
相关文章:
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 野牛/flex:计算器将双精度值解释为整数,所以我添加了 YYSTYPE 双精度 #define 但我有多个编译错误
- C++/错误:表达式必须具有整数或无作用域枚举类型
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- C++字符串到整数错误值可以反转吗?
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 自定义哈希表实现-将字符串映射到整数时出现内存错误
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- C++ 在方程中使用变量;错误:表达式必须具有整数或无作用域枚举类型及其他
- 谷歌启动2018,测试用例中的整数错误
- 使用递归函数 (c++) 将长字符串转换为整数时输出错误
- 运行时错误:有符号整数溢出:964632435 * 10 无法在类型 'int' 中表示
- 将矢量的整数内容打印为字符串会导致分割错误
- 错误:ISO C++禁止在指针和整数 [-fallowive] 之间进行比较
- 将整数数组作为参数传递C++错误
- 如何从返回有符号整数的函数返回错误
- 错误:整数常量的后缀Ui64无效