C++ c+中的整数溢出+;
你能解释为什么我在第一个代码中得到一个整数溢出,而在第二个代码中没有吗C++ c+中的整数溢出+;,c++,integer-overflow,C++,Integer Overflow,你能解释为什么我在第一个代码中得到一个整数溢出,而在第二个代码中没有吗 #include<bits/stdc++.h> using namespace std; #define ch "\n" int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int g = (1000000 * 2) * 1000000; // long long int
#include<bits/stdc++.h>
using namespace std;
#define ch "\n"
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int g = (1000000 * 2) * 1000000;
// long long int k = f * 1000000;
cout << g % 10000003;
return 0;
}
(1000000*2)*1000000
中的所有文字都是int
类型,编译器警告您这会溢出平台上的int
将此表达式的结果指定给其他类型并不重要
一种解决方案是使用(2ll*1000000)*1000000
,这将强制对其他术语进行隐式转换。在(1000000*2)*1000000
中的所有文字都是int
类型,编译器警告您这会溢出平台上的int
将此表达式的结果指定给其他类型并不重要
一种解决方案是使用
(2ll*1000000)*1000000
,强制隐式转换其他术语。请注意,这是一个警告,而不是错误。这是一个警告,您应该将其视为错误,但区别仍然是相关的,因为警告可能会丢失,错误不会注意这是一个警告,而不是错误。这是一个警告,您应该将其视为错误,但区别仍然是相关的,因为警告可能会被忽略,而不是错误
#include<bits/stdc++.h>
using namespace std;
#define ch "\n"
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int f = 1000000 * 2;
long long int k = f * 1000000;
cout << k % 10000003;
return 0;
}
warning: integer overflow in expression of type 'int' results in '-1454759936' [-Woverflow]
8 | long long int g = (1000000 * 2) * 1000000;
| ~~~~~~~~~~~~~~^~~~~~~~~
[Finished in 0.8s]