Thread: Simple Command Line Arguments Exercise.

1. Simple Command Line Arguments Exercise.

I tried the exercise 5 on page 312 of King's Book C programming A modern approach second edition.

Write a program named sum.c that adds up its command line arguments which are assumed to be integers . Running the program by typing :

sum 8 24 62 should produce the following input

Total : 94

Hint: Use the atoi function to convert each command line argument from string form to integer form

My solution is :

Code:
```#include<stdio.h>
#include<stdlib.h>

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

for( i = 1; i < argc; i++)
sum += atoi(argv[i]);

printf("Total: %d\n" , sum);

return 0;
}```
It is running good without problems but I am wondering because it is very small solution with few lines of code . Is there something wrong???? I posted it in order to take some other opinion.

Mr Lnx

2. Looks good to me.

3. Yes, looks fine. As an added exercise, remove the assumption that all arguments are integers and ignore the ones that aren't.

4. Nice idea by Prelude. cctype can be useful.

5. Originally Posted by Prelude
Yes, looks fine. As an added exercise, remove the assumption that all arguments are integers and ignore the ones that aren't.
You mean the string sum\0 the name of the program in argv[0] . I am thinking a way to discard it because I don't use it somewhere.....

6. No, Prelude wants to say that you may now implement your code, without assuming that "command line arguments which are assumed to be integers", from your exercise.

In other words, until now, you assume that the input is something like this : foo 3 4 8 9 6
Now, you may assume that the input can be this : foo t 4 h 5 . In this case, I would add 4 and 5 and discard letters t and h (you could add up their ascii value, but I can not see the point).

7. Originally Posted by std10093
No, Prelude wants to say that you may now implement your code, without assuming that "command line arguments which are assumed to be integers", from your exercise.

In other words, until now, you assume that the input is something like this : foo 3 4 8 9 6
Now, you may assume that the input can be this : foo t 4 h 5 . In this case, I would add 4 and 5 and discard letters t and h (you could add up their ascii value, but I can not see the point).
Code:
```#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

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

for( i = 1; i < argc; i++)
{
if ( isalpha( *argv[i] ) )
continue;

sum += atoi(argv[i]);
}

printf("Total: %d\n" , sum);

return 0;
}```
I think is what we want. Isn't it???

However I have some questions ...

1. If char str[] = ''11"; then atoi(str) converts 11\0 to 11 ? from string form to integer form I saw that if we give a string of alphabetic characters in atoi will return 0 for example int x = atoi("Hi"); // 0

2. How int main (argc , *argv[]) reads the arguments from the keyboard???? It seems like scanf.... for example

Code:
```....

printf("Give x and y: ");
scanf("%d%d" , &x , &y);

printf(" %d , %d " , x , y);```
INPUT: 234 23tak
OUTPUT: x is 234 y is 23

and

Code:
```#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>

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

for( i = 1; i < argc; i++)
{
/*if ( isalpha( *argv[i] ) )
continue;

sum += atoi(argv[i]);*/

printf(" %d " ,atoi(argv[i]));
}

///printf("Total: %d\n" , sum);

return 0;
}```
INPUT : ./sum 23wtwttwtw 22aaaa 1

OUTPUT: 23 22 1

8. That works, except you need to cast to unsigned char when using is* and to* functions (see their man-pages for more information). Alternatively, you could use strtol with a second parameter for better error reporting, which will also allow you to handle arguments that start with digits but end with letters. I'd say your program is sufficient for the exercise at hand, but another issue you might face is overflow since int has a limited range. If you want a challenge, try getting it to work without that limitation.

edit:
By the way, scanf doesn't read anything directly from the keyboard, it reads, and optionally performs conversions on, data from a stream called stdin, which typically buffers data that *may* come from a keyboard. The arguments received by main are completely different; they're not read from a stream, they're received by the C environment at the beginning of your program's interpretation.