无法在32位平台上为64位整数赋值

前端之家收集整理的这篇文章主要介绍了无法在32位平台上为64位整数赋值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从64位平台切换到32位平台(两者都是CentOS)后,我得到的整数常量对于以下代码行的’long’类型错误来说太大了
uint64_t Key = 0x100000000;

铸造价值并没有帮助.我究竟做错了什么?

谢谢

解决方法

您需要使正确类型的整数常量.问题是0x100000000被解释为int,并且转换/赋值没有帮助:常量本身对于int来说太大了.您需要能够指定常量是uint64_t类型:
uint64_t Key = UINT64_C(0x100000000);

会做的.如果您没有UINT64_C,请尝试:

uint64_t Key = 0x100000000ULL;

事实上,在C99(6.4.4.1p5)中:

The type of an integer constant is the first of the corresponding list in which its value can be represented.

并且没有任何后缀的十六进制常量列表是:

int
long int unsigned int
long int
unsigned long int
long long int
unsigned long long int

因此,如果您在C99模式下调用编译器,则不应该收到警告(感谢Giles!).

原文链接:https://www.f2er.com/c/117417.html

猜你在找的C&C++相关文章