小编Anu*_*sad的帖子

如何在C中汇总所有命令行参数?

我有一个任务.该程序将打印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)

c syntax-error atoi command-line-arguments

7
推荐指数
3
解决办法
1790
查看次数

标签 统计

atoi ×1

c ×1

command-line-arguments ×1

syntax-error ×1