#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
// 打印参数个数
printf("Number of arguments: %d\n", argc);
// 打印每个参数
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
// 将参数转换为整数并计算和
if (argc >= 3) {
int num1 = atoi(argv[1]); // 将第一个参数转换为整数
int num2 = atoi(argv[2]); // 将第二个参数转换为整数
int sum = num1 + num2;
printf("Sum of %d and %d is %d\n", num1, num2, sum);
} else {
printf("Not enough arguments. Please provide two integers.\n");
}
return 0;
}
$ ./a.out 123 345
Number of arguments: 3
Argument 0: ./a.out
Argument 1: 123
Argument 2: 345
Sum of 123 and 345 is 468