开发者

What are the arguments to main() for?

Everytime I create a project (standard command line utility) with Xcode, my main function starts out looking like this:

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

What's all this in the parenthesis? Why use this rather开发者_运维问答 than just int main()?


main receives the number of arguments and the arguments passed to it when you start the program, so you can access it.

argc contains the number of arguments, argv contains pointers to the arguments. argv[argc] is always a NULL pointer. The arguments usually include the program name itself.

Typically if you run your program like ./myprogram

  • argc is 1;
  • argv[0] is the string "./myprogram"
  • argv[1] is a NULL pointer

If you run your program like ./myprogram /tmp/somefile

  • argc is 2;
  • argv[0] is the string "./myprogram"
  • argv[1] is the string "/tmp/somefile"
  • argv[2] is a NULL pointer


Although not covered by standards, on Windows and most flavours of Unix and Linux, main can have up to three arguments:

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

The last one is similar to argv (which is an array of strings, as described in other answers, specifying arguments to the program passed on the command line.)

But it contains the environment variables, e.g. PATH or anything else you set in your OS shell. It is null terminated so there is no need to provide a count argument.


These are for using the arguments from the command line -

argc contains the number of arguments on the command line (including the program name), and argv is the list of actual arguments (represented as character strings).


These are used to pass command line paramters.

For ex: if you want to pass a file name to your process from outside then

myExe.exe "filename.txt"

the command line "filename.txt" will be stored in argv[], and the number of command line parameter ( the count) will be stored in argc.


main() is a function which actually can take maximum three parameters or no parameters. The following are the parameters that main() can take is as follows:-

1) int argc : It holds the number of arguments passed (from the command prompt) during the execution of program or you can say it is used ot keep a track of the number of variables passed during the execution of program. It cannot hold the negative value. Eg. If you pass your executable file "./a.out" then that will be considered as a parameter and hence argc value will be 0 i.e 1 value is passed.

2) char *argv[] : it is an array of character pointers which holds the address of the strings(array of characters) that are passed from the command prompt during execution of program. Eg. in above example if you wrote argv[argc] i.e argv[0] in cout then it will give ./a.out as output.

3) char*envp[] : it is also an array of character pointer which is used to hold the address of the environments variables being used in the program. Environment variables are the set of dynamic named value that can affect the way the running process will behave on the computer. For example running process want to store temporary files then it will invoke TEMP environment variables to get a suitable location.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜