c command line arguments

Command Line Arguments

The function main() can accept two parameters. The defination of th main() when it accepts parameters

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

OR

int main(int argc, char **argv) { /* … */ }

in the above example the first parameter is of type int and the second one is an array of pointers to strings.and these parameters are conventionally name argc(argument counter) and the argv(argument vector) and it is used to access the arguments supplied at the command prompt. lets see how it is work first the program is compiled and an executable file is created. talk about In DOS name of the executable file as same as the name of the program and and it has the .exe extension. but in UNIX the of the executable file is named as a.out

so when the program is executed at the command prompt and arguments are supplied to it. so here the first argument is always the name of executable file. so in the above example the parameters argc and argv can be used to access the command line arguments. The parameter argc represent the number of arguments in command line. so all the argiments supplied in the command line are stored internally as string and their address are stored in the array of pointers named argv lets take the example to understand

/* program to understand command line arguments */
#include< stdio.h >
int main(int argc, char *argv[])
{
int i;
printf("the argc=%d \n",argc);
for(i=0;i< argc;i++)
{
printf("argv[ %d ]=%s\n",i,argv[i]);
return 0;
}

for the above example suppose program name is firstprog.c and then it is executed on the command prompt as firstprog you are2 intelligent