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

Anu*_*sad 7 c syntax-error atoi command-line-arguments

我有一个任务.该程序将打印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 sum = 0, counter;

    for (counter = 1; counter < argc; counter++) {
        // Changed the arithmetic condition
        sum = sum + atoi(argv[counter]);
        // Removed the atoi from sum variable
    }
    printf("Sum of %d command line arguments is: %d\n", argc, sum);
}
Run Code Online (Sandbox Code Playgroud)

Iha*_*imi 12

因为你在迭代直到counter == argc,你将NULL指针传递给atoi()它,这很简单,只需要依赖于argv数组有一个NULL哨兵的事实,然后这样做

/* Printing sum of all command line arguments*/
#include <stdlib.h> /* For `atoi()' */
#include <stdio.h>  /* For `printf()' */

int main(int argc, char *argv[])
{
    int sum;
    sum = 0;
    for (int counter = 1; argv[counter] != NULL; ++counter)     {
        sum += atoi(argv[counter]);
    }
    printf("Sum of %d command line arguments is: %d\n", argc, sum);
}
Run Code Online (Sandbox Code Playgroud)

请注意,这atoi(sum)是未定义的行为,因为它sum是一个int并且不是有效指针.虽然atoi()会尝试取消引用它.

最后,包括stdlib.h中aoti().我知道你没有包含它,因为我在编译器上启用了警告,它警告我sum隐式定义.它可能会起作用,但仅仅因为未定义的行为就是这样,未完成.

另外,请注意,无法知道传递的参数是否为整数,因为atoi()无法执行错误检查.您可能希望使用atoi()而不是检查所有值是否为整数.

所以...这就是你如何编写这个程序更强大的版本

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int sum;
    sum = 0;
    for (int counter = 1; argv[counter] != NULL; ++counter)     {
        char *endptr;
        sum += strtol(argv[counter], &endptr, 10);
        if (*endptr != '\0') {
            fprintf(stderr, "error: the `%d-th' argument `%s', is not a valid integer\n", counter, argv[counter]);
            return EXIT_FAILURE;
        }
    }
    printf("sum of %d command line arguments is: %d\n", argc, sum);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

编辑:解决此评论

atoi()例如,如果您通过其中一个strtol()函数执行程序,则有可能.在这种情况下,您必须在开始循环之前进行检查,或者argc == 0在最后一个之后是一个元素,即超出范围.

  • @ChrisTurner:不是很糟糕. (2认同)

Som*_*ude 7

其中特别指明,argv[argc]永远是一个空指针.你循环太多,并传递这个空指针atoi,导致未定义的行为.

改变你的循环条件counter < argc.

并且sum已经整数,您不需要将其转换为整数atoi.这atoi(sum)将导致未定义的行为,因为第一次迭代将传递零,这也可以被视为空指针.atoi


Bat*_*eba 6

的最后一个元素argv定义NULL,和第一个是永远的程序名.因此,您可以将代码减少到

#include "stdio.h"

int main(int argc, char *argv[])
{
    int sum = 0;
    for (int i = 1; argv[i]; ++i){
        sum += atoi(argv[i]);
    }
    printf("Sum of %d command line arguments is: %d\n", argc, sum);
}
Run Code Online (Sandbox Code Playgroud)

在您的代码中,最终迭代的行为atoi(sum)和原因argv[argc]将是未定义的.