Command Line Parameters


The empty paranthesis of function main() may contain special arguments that allow parameters to be passed to main() from the operating system level.
There are two such arguments, which are traditionally called argc and argv respectively.
The first of the argument argc must be an integer variable and the second argument argv is an array of pointers to strings. Each element in this array will represent a parameter that is passed to main().
The value of argc will indicate the number of parameters passed from the operating system level.



Example:
main (int argc, char *argv[ ])
{
:
:
}

The execution of program is normally initiated by specifying the name of the program at the operating system level.
The program name is interpreted as an operating system command.
Hence the line in which it appears refers to as command line.

The parameters must follow the program name on the command line in order to pass one or more parameters to the program when the program execution is initiated.


Syntax:
program name parameter1 parameter2 ...................parameter n

The individual parameters must be separated by one another either by blank spaces or by tabs.
Blank spaces can be included within a parameter by enclosing the entire parameter within quotation marks.



The program name may be stored as the first item in argv followed by each of the parameters.
Hence, if the program name is followed by n parameters there will be n+1 entries in argv ranging from argv[0] to argv [n].
Moreover, argc will automatically be assigned the value n+1.



Note:
The value for argc is not supplied explicitely from the command line.

Example:
#include <stdio.h>
# include<conio.h>
main (int argc, char *argv[])
{
int count;
printf ("Number of arguments %d \n", argc);
for (count = 0; count <argc;count++)
{
printf ("argv[%d] = %s\n", count,argv[count]);
}
getch( );
}


If the input given at the command prompt is > cmdarg1 Suvarni Krithika Gokul then the out put will be as shown below.



Note:
  1. Create an exe file of the program i.e. after compiling the program go for the make option and create an exe file.
  2. Go to DOS prompt. Be in the proper directory if the path is not set and type in the filename (for example cmdarg1 is the file name) and the parameters (cmdarg1 Suvarni Krithika Gokul). Here Suvarni, Krithika and Gokul are the parameters.

If the command line parameters are given as > cmdarg1 Suvarni “Krithika Gokul”
The output will be:



Program to read a data file and display its contents. Use command line parameters to pass in the file name.
# include<stdio.h>
#include <conio.h>
main (int argc, char *argv[])
{
FILE *ptvar;
char c;
printf ("Number of arguments passed %d\n", argc);
if ((ptvar = fopen(argv[1],"r")) ==0)
printf ("Error opening the file \n");
else
{
do
{
c=getc(ptvar);
putchar(c);
} while(c!= EOF);
fclose (ptvar);
}
getch();
}

We have given the parameter as >cmdarg2 cmdarg1.c
The Output is:



The parameter cmdarg2 is the filename of the program and cmdarg1.c is the name of the file of which we want to display the contents.
You can see the output we are reading the contents of the file cmdarg1.c (our earlier program).




Dynamic Memory Allocation<< Previous
Next >>Some C Programs

Our aim is to provide information to the knowledge seekers.


Support us generously

comments powered by Disqus


















Footer1