Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning c4307 integral constant overflow in C

I have this operation (8 * (512 * 786432)) and the product is 3221225472

I tried to use it with variables like longlong, unsigned long

But the compiler throw me an error

c4307 integral constant overflow

and I need the result for use it with functions, how can I fix it? or what variables can work for large numbers?

regards

like image 598
Makuvex Linux Avatar asked Jan 31 '14 23:01

Makuvex Linux


2 Answers

The expression (8 * (512 * 786432)) has type int and it will overflow on 32-bit systems. Assigning it to a variable of type long does not change the fact that the value has already overflowed. You can fix this by annotating the numbers.

long x = (8L * (512L * 786432L));

You only need to put the L on one of the numbers, since it will force results to also have type long.

This assumes that long is 64-bit, which is true on most systems but not Windows. You will need LL (or i64) on Windows.

like image 196
Dietrich Epp Avatar answered Oct 18 '22 05:10

Dietrich Epp


Try to use

(8 * (512ull * 786432)) 
like image 41
Vlad from Moscow Avatar answered Oct 18 '22 07:10

Vlad from Moscow