Saturday, May 7, 2016

Command Line Arguments


We have seen main() function in all the C programs so far. But we have seen the main() function always without arguments. In this section, we will see how we can pass main() function with arguments. Such an argument which is passed in main() function is called command line argument.

Often we need to have some input,to be entered by the user, in some program . To do this,we usually perform scanf() or gets() as appropriate. But many a times, passing the input through the command line arguments can do the job easily. Say ,for example, you need to pass some 'n' input paramaters, and this 'n' need not be the same each time you execute the program.You would obviously not want to have a separate program for each of these scenarios,nor you would want the user to first input 'n' , and then enter the 'n' number of inputs, which can be annoying. Command line arguments can make the job quite simple in such a case without annoying the user in any way.

The syntax is as below:-

void main(int *argc , char *argv[])

argc is the total number of arguments being passed at the command line including the name of the executable (a.out,in general).

char *argv[] is the array of character pointers which points to all the arguments passed.

argv[0] points to the name of the executable (a.out,in general), argv[1] points to the actual first argument passed and so on.


Check out one simplest and shortest program-


Here, we have not considered argv[0] because it points to the name of the binary executable. Total number of arguments is argc - 1 because argc also includes the binary executable in total count.

Output

 

We passed 10 arguments in total containing a mix of numbers and strings. So, one argument is differentiated from the other with the help of a space in between the two arguments.

What if arguments are multi-word sentences??

Then we can pass such parameters in double quotes(“ “) or single quotes (' ').



Wherever there are double quotes or single quotes , the entire text within the quote is referred to as a single argument. If there are no quotes, then again the differentiation between two arguments takes place with the help of space.

Each of the command line arguments are strings, so we can't perform arithmetic operation on them directly, even though we pass numbers as the arguments. Proper conversion from String to int needs to be done before doing this. There are several library functions to do that viz., atoi,atol,strtol, etc.

Let's see an example-

 

Please remember that atoi only converts a string to integer only if it's a number. The above program will make it clear when we pass different types of arguments on command line.




Let's mess with the input arguments now




We observe that atoi on a string of characters has rendered value 0 for each of them. Hence , the final output that we are getting is wrong.

<<<Try doing the same program without performing atoi operation.>>>

Explore atoi,strtol,etc.

man atoi
man strtol

This was just a basic introduction to command line arguments. It is used in vast number of applications in C.



2 comments: