Invalid product of two INT_MAX numbes in C / C ++

In my case, the product of two INT_MAX numbers is equal 296447233, which is wrong.

long long int product = 0;
product = 2137483647 * 2137483647;
printf("product: %lli\n", product);

What am I doing wrong and how to fix it ?? Thank!

+5
source share
2 answers

Both of 2137483647are of type int. Thus, they retain this type and overflow.

Make them long longs:

product = 2137483647LL * 2137483647LL;

or cast:

product = (long long)2137483647 * 2137483647;
+14
source

Try

product = 2137483647LL * 2137483647LL; 

for the compiler to process numbers for how long are long

+6
source

All Articles