Cannot assign value to 64-bit integer on 32-bit platform

After switching from a 64-bit to a 32-bit platform (both of them CentOS), I get an integer constant is too large for 'long' type error for the next line of code

 uint64_t Key = 0x100000000; 

The value of the value does not help. What am I doing wrong?

thanks

+6
c types 64bit 32-bit 32bit-64bit
source share
3 answers

You need to make an integer constant of the correct type. The problem is that 0x100000000 interpreted as int , and casting / assign does not help: the constant itself is too large for int . You must be able to specify that the constant is of type uint64_t :

 uint64_t Key = UINT64_C(0x100000000); 

will do it. If you do not have UINT64_C , 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 a list for hexadecimal constants without any suffix:

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

So, if you called your compiler in C99 mode, you should not receive a warning (thanks Giles!).

+10
source share

What you wrote is great for C99 (if you have #include d <stdint.h> ) ΒΉ. So it looks like you used your compiler in C89 mode, not C99 mode. C89 does not guarantee the availability of a 64-bit type, but this is a general extension.

Since you are running Linux, your compiler is supposedly gcc. Gcc supports 64-bit long long on all platforms, even in C89 mode. But you may need to explicitly declare the constant as long long :

 uint64_t Key = 0x100000000ull; 

( ll means long long ; u means unsigned and optional here). Alternatively, you can run gcc in C99 mode with gcc -std=c99 .

ΒΉ for language lawyers: and have an integral type with exactly 64 bits of value.

+4
source share

There is no guarantee, but you can try:

 uint64_t Key = 0x100000000ULL; 
0
source share

All Articles