main函数参数求俩个数字之和
时间: 2024-06-11 12:08:21 浏览: 41
main函数的两个参数
以下是一个示例程序,它接收两个数字作为参数,并打印它们的和:
```c++
#include <iostream>
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " <num1> <num2>\n";
return 1;
}
int num1 = std::stoi(argv[1]);
int num2 = std::stoi(argv[2]);
int sum = num1 + num2;
std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << std::endl;
return 0;
}
```
在命令行中运行程序时,应提供两个数字作为参数。例如:
```
./sum 10 20
```
将输出:
```
The sum of 10 and 20 is 30
```
阅读全文