What is a command line argument in C language?

An executable instruction that performs a task for OS is called a command. These commands are issued from the prompt of OS.

The arguments that are associated with the commands are as follows −

  • argc - argument count.

  • argv - argument vector.

argc − It holds the total number of arguments passed from the command prompt.

argv − It is a pointer to an array of character strings contains names of arguments.

For example,

c: |> sample. Exe hello how are you


  • argc = 5

  • argv[0] = sample.exe

  • argv[1] = hello

  • argv [2] = how

  • argv[3] = are

  • argv[4] = you


Following is the C program for command line argument −

main ( int argc, char *argv[ ]){
   int i;
   clrscr( );
   printf (" no. of arguments at command p = %d", argc);
   printf (" arguments given at prompt are 
");    for ( i = 1; i <argc; i++)       printf ("%s
", argv[i]);    getch( ); }


To run a C program with command-line arguments −

  • Compile the program

  • Run the program

  • Go to the command prompt and give the input as shown below.

c:|> sample.exe hello how are you.
No. of arguments given at prompt is = 5
Arguments given at command prompt are: