What is Command Line Argument In C Programming?

Command line argument is a parameter supplied to the program when it is invoked. Command line argument is an important concept in C programming. It is mostly used when you need to control your program from outside. command line arguments are passed to main() method.

Syntax :

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

Here argc counts the number of arguments on the command line and argv[ ] is a pointer array which holds pointers of type char which points to the arguments passed to the program.


Example for Command Line Argument

#include <stdio.h>
#include <conio.h>
int main( int argc, char *argv[] )
{
  int i;
  if( argc >= 2 )
   {
    printf("The arguments supplied are:\n");
    for(i=1;i< argc;i++)
    {
     printf("%s\t",argv[i]);
    }
   }
   else
   {
     printf("argument list is empty.\n");
   }
 getch();
 return 0;
}

Remember that argv[0] holds the name of the program and argv[1] points to the first command line argument and argv[n] gives the last argument. If no argument is supplied, argc will be one.