COMMAND LINE
ARGUMENTS:
A
command line argument is a parameter supplied to the program when the program
is invoked. The parameters from command line can be passed to the program
through two arguments in main function. They are argc,argv.
Argc:- argc is an argument counter which contains
the arguments on the command line.
Argv:- argv is an argument vector which represent
an array of character pointers that point to the command line arguments. The
first argument should be the name of the program In order to access command
line arguments; we must declare main function and its parameters as follows:
Syntax:-
void main(argc,argv) main(int
argc,char *argv[])
int argc; {
char argv[]; (or) /*body of the program*/
{ }
/*body of the program*/
}
Ø
A
Program to print the arguments passed using the command line arguments:
void main(int argc,char argv[])
{
int I;
printf(“number of arguments=%d”,argc);
printf(“the arguments are\n”);
for(I=1;I<argc;I++)
{
printf(“%s\n”,argv[I]);
}
getch( );
}
No comments:
Post a Comment