/* Name: ex13-5.c Purpose: finds the sum of all command line arguments Author: Bill Slough Notes: 1. The atoi() function has been deprecated and should not be used for new code. Use of strtol() is the preferred approach. 2. No error checking is performed; arguments are assumed to be integers! 3. Although not required, this program displays the value of the argc/argv data structure. */ #include #include int main(int argc, char *argv[ ]) { int i; int sum; /* how many arguments appear on the command line? */ printf("argc = %d\n", argc); /* process each command line argument, skipping the 0th argument */ i = 1; sum = 0; while (argv[i] != NULL) { printf("argv[%d] = \"%s\"\n", i, argv[i]); sum += atoi(argv[i]); i++; } printf("sum = %d\n", sum); return 0; }