Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的整数溢出+;_C++_Integer Overflow - Fatal编程技术网

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]