C++中int和long long特别容易被忽略的点,在做乘法的时候即使单个变量在int范围内,如果乘积超了int,也需要将乘数定义为longlong 否则会出错

C++中int和long long特别容易被忽略的点,在做乘法的时候即使单个变量在int范围内,如果乘积超了int,也需要将乘数定义为longlong 否则会出错
请看示例:

#include 
using namespace std;
int main(){
    int i = 100000000;
    long long j = 100000000;
    cout << i*i << " " << sizeof(i*i) << " " << (long long)i*i << " " << sizeof((long long)i*i) << " " << i << endl;
    cout << j*j << " " << sizeof(j*j) << " " << int(j*j) << " " << sizeof(int(j*j)) << " " << j << endl;
    return 0;
}

注意里面类型转换意义不同!
结果:
在这里插入图片描述