How to define command line arguments

The main functions can have arguments passed which are called as command line arguments. There are two command line arguments:

Argument count denoted by argc and
Argument vector denoted by argv

The argc is an integer variable which denotes the number of parameters passed and argv is pointer to array of character strings.

The syntax is as follows:

main( int argc , char * argv[ ])
{
…..
…..
}

It can also be returned as

main(argc,argv)
int argc;
char * argv[];
{
…….
…….
}

After the program is executed the first argument entered in prompt would be the name of the program which is to be executed followed by parameters. Say for example if the program name of execution is sample and say 3 values are to be passed namely a1,a2,a3 it is given as follows:

sample a1 a2 a3

So in this case argc have value as 3 which is the number of parameters passed and argv would have values as

argv[0]= sample
argv[1]=a1
argv[2]=a2
argv[3]=a3

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It