Saturday, February 19, 2022

[SOLVED] long long is 8 bytes, but I get integer overflow?

Issue

Suppose

  long long b = 5*1024*1024*1024; // 5 gigs, small enough for 64 bits
  printf ("%lu\n",sizeof(long long)); // prints 8 (bytes) = 64 bits

but the compiler complains:

  warning: integer overflow in expression [-Woverflow]

Why does it overflow, what am I missing?


Solution

Because the numbers on the right hand side are of type int, not long long, so int arithmetic is performed in the expression, leading to an overflow.

If you add LL to one of them, it'll promote them all.



Answered By - teppic
Answer Checked By - David Marino (WPSolving Volunteer)