我有一个任务.该程序将打印C中所有命令行参数的总和.我尝试编译它的代码,但在控制台中传递参数后抛出错误.下面是代码.
/* Printing sum of all command line arguments */
#include <stdio.h>
int main(int argc, char *argv[]) {
int sum = 0, counter;
for (counter = 1; counter <= argc; counter++) {
sum = atoi(sum) + atoi(argv[counter]);
}
printf("Sum of %d command line arguments is: %d\n", argc, sum);
}
Run Code Online (Sandbox Code Playgroud)
编译后显示Segmentation fault(core dumped)错误.您的经验可以解决我的问题.
以下是我编辑的代码:
/* Printing sum of all command line arguments*/
#include <stdio.h>
#include <stdlib.h> // Added this library file
int main (int argc, char *argv[]) {
int …Run Code Online (Sandbox Code Playgroud)