new*_*int 5 c c++ printf overflow multiplication
就我而言,两个INT_MAX数字的乘积296447233是不正确的.
long long int product = 0;
product = 2137483647 * 2137483647;
printf("product: %lli\n", product);
Run Code Online (Sandbox Code Playgroud)
我做错了什么,以及如何纠正它?谢谢 !
Mys*_*ial 14
你们2137483647都属于这两种类型int.所以他们保持这种类型并溢出.
让他们long long:
product = 2137483647LL * 2137483647LL;
Run Code Online (Sandbox Code Playgroud)
或演员:
product = (long long)2137483647 * 2137483647;
Run Code Online (Sandbox Code Playgroud)