Explaining (int argc, char * argv[])

You all have often came across this term while going through a c or c++ code.I just want to explain this in brief especially for the newbies who find it difficult to understand this concept.

Here argc represents the number of arguments(command line argument) and argv is the argument vector pointing it.
Suppose I am compiling a c code ‘test.c’ so what i give at command line is

$ gcc test.c -o test
$ ./test                                                           //for running executable file test

so here argc=1 as no of command line arguments here is one and argv[0]=./test

similarly if executable is run as

$ ./test <arg1> <arg2>

then here argc=3 and argv[0]=./test ,argv[1]=arg1 and argv[2]=arg2

Below is the c code i have written try it for better understanding of this concept.Have lots of fun with c codes 🙂

main(int argc, char* argv[])
int i;
printf("argc = %d\n", argc);
for (i = 0; i < argc; i++)
printf("argv[%d] = %s\n", i, argv[i]);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s