Command line argument programming in C

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

This method is used in order to specify the input data required for the given program while calling it from command line.

Here,two parameters are specified to the main function,argc and argv.

argc is integer type variable,which indicates total parameters specified on command line.

argv is a char type pointer array,which contains actual parameter specified on command line.

E.g. c:\ lcm 10 20

Here,program name is lcm,argc value will be 3 and the content of argv array will be as follows :

+----+             address+----+

| 100 | ---->    100    | lcm |

+----+                       +----+

| 200 | ---->    200    | 10  |

+----+                       +----+

| 300 | ---->    300    | 20  |

+----+                       +----+


In main function no size is specified for argv array,but required slots are created and initialized automatically while loading the program for execution.

Remember,each argument is stored in ascii (char) format in argv.If numeric parameters are required ,conversion is required from char to integer or long or float or double.

If command line argument is to be modified and current allocated bytes are not sufficient,it is copied in local variable.

In short,to avoid any problem first copy the command line argument variable in local variable before processing.

While writing a program assume that input parameter are already available in argv array.Use the value of argc to take the action on invalid number of arguments,

Examples of Command Line Argument