1
int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
            printf("0\n");
            return (0);
    }
    else {
        for (i = 1; i < argc; i++) {
            if (isdigit(argv[i]) == 0) {
                printf("Error\n");
                return (1);
            }
            sum += atoi(argv[i]);
        }
    }
    printf("%d\n", sum);
    return (0);
}

When I give its executable any argument/s, the above code always outputs "Segmentation fault (core dumped)" which means that I'm trying to access a memory location which I don have access to.

I think that putting the condition (i > argc) should mean that I do make sure that argc is of value allows me to access argv[argc - 1], yet I don't see where the error comes from and this is my where i need some help to understand.

I am expecting the program to add the digits arguments and outputs its sum and to print "Error" once if there a non digit argument

paulsm4
  • 114,292
  • 17
  • 138
  • 190
fromesoli
  • 11
  • 1
  • 3
    `isdigit(argv[i][0])` ?? isdigit operates on chars, not strings of chars. (Although even that test may cause issues, in case you pass in say: "9abc"... – robthebloke Jul 10 '23 at 02:45
  • 2
    Don't forget to include ``, ``, and `` as well. Don't forget to enable warnings as well. The bad argument to `isdigit` should have caused a warning. – Tom Karzes Jul 10 '23 at 02:57

1 Answers1

2

Here are a few modifications to the code you posted:

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

int main(int argc, char *argv[])
{
    int i;
    int sum = 0;

    if (argc == 1) {
       printf("Please enter numbers to sum.  EXAMPLE: myapp 2 4 6");
       return 1;
    }
    else {
        for (i = 1; i < argc; i++) {
            if (!isdigit(argv[i][0])) {
                printf("Error: %s isn't numeric!\n", argv[i]);
            }
            else {
                sum += atoi(argv[i]);
            }
        }
    }
    printf("%d\n", sum);
    return (0);
}
  • You should include headers (e.g. #include <stdio.h>) for all the functions you use.
  • isdigit() accepts a character, not string.
  • If argc isn't > 1, provide a meaningful message to the user, and return a nonzero result to the system.
  • You don't necessarily need to exit for bad input: you might wish to somehow flag the error, and continue. It's up to you.
  • But if you do exit, return a nonzero result to the system. For example, that could allow a shell script to determine whether or not your app returned a valid sum.
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 2
    Nit - superfluous `else { ... }` after `return 1;`. (at least ding OP for it `:)` – David C. Rankin Jul 10 '23 at 05:12
  • 2
    A bigger "nit" is using "isdigit()" to test for "numeric". Just because the first character happens to be a digit doesn't mean the rest of the string is a valid integer. [atoi()](https://linux.die.net/man/3/atoi) cannot detect errors. A better approach would be to substitute something like "strtol()". For both "isdigit()" and "atoi()". [strtol()](https://linux.die.net/man/3/strtol) can both *convert* the string to an integer, *and* give a meaningful error return if the string isn't a valid integer. – paulsm4 Jul 10 '23 at 14:44
  • Most agreed. Maybe a link to [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) is in order? – David C. Rankin Jul 11 '23 at 04:18