Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

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

Suppose

  long long b = 5*1024*1024*1024; // 5 gigs, small enough for 64 bits
  printf ("%lu
",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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...