long long is 8 bytes, but I get integer overflow?

后端 未结 1 783
梦毁少年i
梦毁少年i 2021-01-11 12:42

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
<         


        
相关标签:
1条回答
  • 2021-01-11 13:21

    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.

    0 讨论(0)
提交回复
热议问题