Thread: Simple Command Line Arguments Exercise.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    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.

    Thank you in advance

    Mr Lnx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Looks good to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Yes, looks fine. As an added exercise, remove the assumption that all arguments are integers and ignore the ones that aren't.
    My best code is written with the delete key.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Nice idea by Prelude. cctype can be useful.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by Prelude View Post
    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. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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 - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Quote Originally Posted by std10093 View Post
    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. #8
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    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.
    Last edited by Barney McGrew; 01-22-2013 at 10:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments
    By NessValdez in forum C Programming
    Replies: 8
    Last Post: 10-23-2012, 12:45 AM
  2. command line arguments
    By benhaldor in forum C Programming
    Replies: 17
    Last Post: 08-25-2007, 07:04 AM
  3. command line arguments
    By St0rM-MaN in forum C Programming
    Replies: 10
    Last Post: 05-07-2007, 04:40 AM
  4. command line arguments
    By c_cool in forum C Programming
    Replies: 1
    Last Post: 07-08-2004, 06:28 AM
  5. command line arguments
    By singhhome in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 08:54 AM