// This program demonstrates the use of the atoi() function, located in // standard library. This function converts an ascii code to an int // This allows you to take a number from the command line (remember that this // will be as an ascii code, since you store command line arguments as strings!) // and convert that ascii code to an actual number that you can perform // mathematical operations on. // Sasha Vassar Week 5, Lecture 10 // ./atoi 8 9 10 #include // The function atoi() is located in the standard library #include int main (int argc, char *argv[]) { //Remember that the command line arguments are strings, so if you need //to do mathematical operations, you will need to convert them to numbers //You can do this with the help of the atoi() function in //Let's now print out all the command line arguments given and //add them together to give the sum of command line arguments int sum = 0; //Start the index at 1, because 0 is the program name int i = 1; while (i < argc) { printf("The command line argument at index %d (argv[%d]) is %d\n", i, i, atoi(argv[i])); sum = sum + atoi(argv[i]); i++; } printf("The sum of the command line arguments is %d\n", sum); return 0; }