Command Line Arguments in C




Command Line Arguments in C

We can provide values at command prompt using command line arguments. We have to specify the program name followed by values to be sent as arguments .

Command line arguments to main() have two parameters passed as main ( int argc, char *argv[] )

  • argc specifies the number of arguments
  • *argv[] specifies the values passed at command line as arguments .
Program for command line arguments.
#include <stdio.h>
int main ( int argc, char *argv[] )
{
int counter;
printf(“Command line arguments”);
for(counter=0;counter<argc;counter++)
{
puts(argv[counter]);
}
return(0);
}

How to run:

  1. Name of program is supposed to be file.C
  2. Make its executable by compiling it.
  3. Go to Dos Shell from file menu.
  4. You need to type :file this is command line

The output of the program would be :

Command line arguments

d:\TCC\file.EXE  this
is
command
line