Write about Command Line Arguments?


Command line argument is a parameter supplied to a program when the program is invoked.

We know that every C program should have one main fn.& it can also take arguments like other functions.

In fact main can take 2 arguments called argc and argv & the information Contained in the command line is passed on to the program through these arguments, when main is called up by the system.

The variable argc is an argument counter that counts the number of arguments on the command line. The argv is an argument Vector & represents an array of character pointers that point to the command line arguments. The size of this array will be equal to the value of argc.

In order to access the command line arguments, we must declare the main function& its parameters as follows.

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

The first parameter in the command line is always the program name & argv[0] always represents the program name.

Ex:

#include
main ( int argc, char * argv[  ])
{
int count;
printf (“number of arguments-%d”, argc);
for ( count =0, count < argc; count ++)
printf(“\n argv [%d] =%s”, count, argv[count]);
}

Save this program as command.C.

The command line initiating program execution is:

Command.exe SASI DEGREE COLLEGE

Then the program will be executed & display the following output

argc-4

argv[0]=command.exe

arg[1]=SASI

argv[2]=DEGREE

argv[3]=COLLEGE




Related

C Language 5779743632924378910

Post a Comment

emo-but-icon

item