Monday, May 24, 2010

In C language,what are the parameters sent to main function?

int main(int argc, char **argv, char **env);

In C language,what are the parameters sent to main function?
Tell you what. I haven't coded a single line since May 2002. That's because I never got another job ever since May 2002.





When I answered this question, I did not refer to any book/manual. I just followed my instincts because right away I knew that the other answers were certainly wrong. Report It

Reply:int main(int argc, char *argv[]);





argc is number of arguments.





argv is array of pointers to "strings"
Reply:The main function definition in C is





void main(int argc, char *argv[]);





Suppose you have and exe file which is created using C program. Assume the name of the file is foo.exe. Now while you run this file you can pass arguments to the exe file like





foo 1 2 sridevi





In this case value of your argc will be 3, as there are 3 parameters passed. argv will store these parameters. so argv[1] = 1; argv[2] = 2; argv[3] = sridevi.





You can not pass these parameters in program. These must be passed at the run time.
Reply:The main function can accept parameter from the command line as arguments.


To accept arguments, you should declare the main function as bellow


int main( int argc, char *argv[] )


In that, argc is the number of argument, include the program name itself, and argv contains values of arguments.


Example:


With the above declaration, the program name is prg1, and if you type in the command line:


%26gt; prg1 abc 333


argc will be 3


and argv[0] = prg1


argv[1] = abc


argv[2] = 333


No comments:

Post a Comment