cgccliterals128-bit

Assigning 128-bit integer in C


When I try to assign an 128-bit integer in gcc 4.9.1, I get a warning: integer constant is too large for its type.

Example Code

int main(void) {
  __uint128_t p = 47942806932686753431;

  return 0;
}

Output

I'm compiling with gcc -std=c11 -o test test.c and I get:

test.c: In function ‘main’:
test.c:2:19: warning: integer constant is too large for its type
   __uint128_t p = 47942806932686753431;
               ^

Am I doing something wrong or is this a bug in gcc?


Solution

  • Am I doing something wrong or is this a bug in gcc?

    The problem is in 47942806932686753431 part, not in __uint128_t p. According to gcc docs there's no way to declare 128 bit constant:

    There is no support in GCC for expressing an integer constant of type __int128 for targets with long long integer less than 128 bits wide.

    So, it seems that while you can have 128 bit variables, you cannot have 128 bit constants, unless your long long is 128 bit wide.

    The workaround could be to construct 128 bit value from "narrower" integral constants using basic arithmetic operations, and hope for compiler to perform constant folding.