After switching from 64-bit to 32-bit platform (both of them are CentOS) I get integer constant is too large for ‘long’ type
error for the following line of code
uint64_t Key = 0x100000000;
Casting the value does not help. What am I doing wrong?
Thanks
You need to make the integer constant of the right type. The problem is that 0x100000000
is interpreted as an int
, and casting / assignment doesn't help: the constant itself is too big for an int
. You need to be able to specify that the constant is of uint64_t
type:
uint64_t Key = UINT64_C(0x100000000);
will do it. If you don't have UINT64_C
available, try:
uint64_t Key = 0x100000000ULL;
In fact, in 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.
and the list for hexadecimal constants without any suffix is:
int
long int unsigned int
long int
unsigned long int
long long int
unsigned long long int
So if you invoked your compiler in C99 mode, you should not get a warning (thanks Giles!).
What you've written is perfectly valid C99 (assuming you've #include
d <stdint.h>
)¹. So it looks like you've invoked your compiler in C89 mode rather than C99 mode. In C89, there is no guarantee that a 64-bit type is available, but it's a common extension.
Since you're on Linux, your compiler is presumably gcc. Gcc supports a 64-bit long long
on all platforms, even in C89 mode. But you may have to explicitly declare the constant as long long
:
uint64_t Key = 0x100000000ull;
(ll
means long long
; u
means unsigned and is optional here). Alternatively, you may want to run gcc in C99 mode with gcc -std=c99
.
¹ for the language lawyers: and have an integral type with exactly 64 value bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With