0

I need to write a program in a single file "sum.c" that accepts multiple integers on the command line, takes the sum of those integers and prints them to stdout. The program must use something like strtol to convert from string to number.

So far my code looks like this:

    #include <stdio.h>

int main (int argc, char *argv[]){
        int a, b, sum;
        int i; //looping through arguments using i

        if (argc<2) {
                printf("Please include at least two integers to get the sum.\n");
                return -1;
        }

a = atoi(argv[1]);
b = atoi(argv[2]);

sum=a+b;

printf(sum);
return (0);

}

This includes an error check to make sure at least two arguments are passed. However my current code only allows for two arguments. I need to figure out how to change this to handle any number of arguments, and to also check that the numbers being passed are ONLY integers and nothing else. I am also still having compilation errors with the original code that I posted here. I have taken a long break from coding so I know it is very poor at the moment.

Updated Code:

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

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

        if (argc<2) {
                printf("Please include at least two integers to get the 
sum.\n");
                exit (-1);
        }

        for (int counter = 1; argv[counter] != NULL; ++counter) {
                sum += atoi(argv[counter]);
        }

        printf("%d\n", sum);
exit (0);
}

How does this look now?

Error I receive when executing:

./sum.c: line 4: syntax error near unexpected token `('
./sum.c: line 4: `int main (int argc, char *argv[]) {'
Brad Bieselin
  • 107
  • 1
  • 11
  • Use a `for` loop that iterates over all the argument. – Barmar Sep 20 '19 at 21:42
  • `printf(sum)` should be `printf("%d\n", sum);` – Barmar Sep 20 '19 at 21:43
  • I updated the code using your recommendations. How does this look now? – Brad Bieselin Sep 20 '19 at 21:48
  • You still need to fix the `printf()` at the end. The loop looks good, although you're not validating that the argument is actually a number. – Barmar Sep 20 '19 at 21:49
  • Oh sorry, I updated that in my code but forgot to add it here. And yes I think for validating the argument, I'll need to run an if statement to check that the arguments are integers? Also when i run this code Im getting an error for line 4 ./sum.c: line 4: syntax error near unexpected token `(' ./sum.c: line 4: `int main (int argc, char *argv[]) {' – Brad Bieselin Sep 20 '19 at 21:51
  • You can use `strtol()` to convert the number and check if it's numeric at the same time. See https://stackoverflow.com/questions/19148611/using-strtol-to-validate-integer-input-in-ansi-c – Barmar Sep 20 '19 at 21:53
  • Thank you!!! I am going to read over that question! – Brad Bieselin Sep 20 '19 at 21:53
  • `exit` is a function, it needs to be `exit(-1);` – Barmar Sep 20 '19 at 21:54
  • I don't get any error on line 4, though. – Barmar Sep 20 '19 at 21:55
  • I’m running this code in Linux. I’m not sure if maybe that’s why I get an error on line 4? – Brad Bieselin Sep 20 '19 at 22:07
  • Shouldn't make a difference. That line is perfectly fine. – Barmar Sep 20 '19 at 22:09
  • Why do you need at least two integers? If one integer is provided, the sum is that integer. If no integers are provided, you can deem the sum is zero. Also, what happens if someone types `your_prog cat dog`? - Your code will report a sum of zero, should it report an error? – Peter Sep 20 '19 at 22:21
  • @Peter, i do need to incorporate error checking for this. So you are correct that if something besides integers are entered it should error and exit. However I have not figured out how to go about this yet – Brad Bieselin Sep 21 '19 at 18:42

2 Answers2

0

The new code is largely fine, except for the error on the call exit -1 which is a bug. The call must be exit(-1).

The reason exit -1 is not generating a compile error is that C allows compilers to accept expressions with no effect as legal (so, the following is legal C code:

static void
useless_func (void)
{
    5;
    exit;
}

This function has two lines that do nothing at all--the value 5 and the value that is the address of the exit function. Note well that calling useless_func() will not result in your program terminating, as the exit function itself is not being called).

You can see that yourself in your program, if you invoke with no arguments, the program doesn't terminate--the sum of 0 will be printed. If you change the line exit -1 to exit 1 you will get a compilation error, as now you no longer have a legal expression (the compiler sees what you have as exit - 1 not exit -1, and the minus sign makes it an expression).

If you are using gcc as your compiler, compiling with -Wall will generate a warning. If you compile with -Wall -Werror (a very good habit to get into) you will get an error.

Also, you ought to end your program with a call to exit(0);

One Guy Hacking
  • 1,176
  • 11
  • 16
  • Thanks for the response and helping me understand! I am still coming across an error after updating the exit codes. I have put this back into the original post if you can take a look for me? – Brad Bieselin Sep 21 '19 at 18:35
  • The new code is largely correct. You have a minor bug in that you will accept a single integer and sum it instead of rejecting it and requiring a minimum of two (as you say you will do. Accepting 1 (or even 0) integers to sum is not inherently a bug. You're just not doing what you say you will do). I don't understand your comment about getting a syntax error on execution. That should not be possible--the syntax is evaluated at compile time. Do you mean a syntax error when compiling? Even then, I don't see any syntax error. (Nor does my compiler--gcc accepts this code just fine) – One Guy Hacking Sep 22 '19 at 04:40
0

C is compiled, then the resulting executable can be executed. You do not run the C source code

./sum.c

That attempts to run the C source code.
You need something like

$ gcc -std=c99 -pedantic -Wall sum.c -osumexe
$ ./sumexe
pmg
  • 106,608
  • 13
  • 126
  • 198