此代码在 xlC 8.0(在 AIX 5.3 上)中编译时会产生错误的结果。
它应该打印 12345
,但打印的是 804399880
。
删除 result
前面的 const
可使代码正常工作。
错误在哪里?
#include <stdio.h>
#include <stdlib.h>
#include <string>
long int foo(std::string input)
{
return strtol(input.c_str(), NULL, 0);
}
void bar()
{
const long int result = foo("12345");
printf("%u\n", result);
}
int
main()
{
bar();
return 0;
}
编译命令:
/usr/vacpp/bin/xlC example.cpp -g
编辑:将上面的 printf 格式字符串更改为“%ld\n”没有帮助。 编辑 2:使用的 AIX 版本是 5.3,而不是 6.1。
最佳答案
xlC 10.0 工作正常,似乎是一个编译器错误
关于c++ - 初始化常量的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2645383/