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

jac*_*hab 6 c 64-bit types 32-bit 32bit-64bit

从64位平台切换到32位平台(两者都是CentOS)后,我得到integer constant is too large for ‘long’ type以下代码行的错误

uint64_t Key = 0x100000000;
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Alo*_*hal 10

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

uint64_t Key = UINT64_C(0x100000000);
Run Code Online (Sandbox Code Playgroud)

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

uint64_t Key = 0x100000000ULL;
Run Code Online (Sandbox Code Playgroud)

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

整数常量的类型是相应列表中可以表示其值的第一个.

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

int
long int unsigned int
long int
unsigned long int
long long int
unsigned long long int
Run Code Online (Sandbox Code Playgroud)

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