Explain command line argument with example and program

 What is Command line argument explain with example and program

 Command Line argument

It is a parameter supplied to a program when the program is invoked.
This parameter may represent a filename the program should process.
We know that every C program should have one main function and that it marks the beginning of the program.
It can also take arguments like other functions.
In fact it can take two arguments argc and argv and the information contained in the command line is passed on to the program through these arguments, when main is called up by the system.

argc  : The variable argc is an argument counter that counts the 
             number of  arguments on the command line.
argv  :  it is an argument vector that represents an array of character
             pointers that points to the command line arguments.
             the size of this array will be eqaul to the value of argc.

For example :

   c:\>program x_file y_file
   argv[0] -> program
   argv[1] -> x_file
   argv[2] -> y_file

In order to access the command line arguments, we must declare the main function and its parametres as follows :

               main ( int argc, char *argv[ ] )
               {
                 …………………..
                 …………………….
                }
The first parameter in the command line is always the program name and therefore argv[0] always represents the program name.

Labels: