6

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

jackhab
  • 17,128
  • 37
  • 99
  • 136

3 Answers3

10

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!).

Mike Pelley
  • 2,939
  • 22
  • 23
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
4

What you've written is perfectly valid C99 (assuming you've #included <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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0

No guarantee, but you might try:

uint64_t Key = 0x100000000ULL;
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111