问题
In my case, product of two INT_MAX numbers is 296447233, which is incorrect.
long long int product = 0;
product = 2137483647 * 2137483647;
printf("product: %lli\n", product);
What I am doing wrong, and how to correct it ?? Thanks !
回答1:
Both of your 2137483647 are of type int. So they stay that type and overflow.
Make them long longs:
product = 2137483647LL * 2137483647LL;
or cast:
product = (long long)2137483647 * 2137483647;
回答2:
Try
product = 2137483647LL * 2137483647LL;
to ensure that the compile treats the numbers as long long
来源:https://stackoverflow.com/questions/9504477/incorrect-product-of-two-int-max-numbes-in-c-c