简体   繁体   中英

Command-line arguments

I am aware that the main function can take two arguments: int argc and char* argv[] . This is well documented. However the main function can also take a third argument. Does anyone one know what this argument is?

It's the environment variables, and have the same type as the normal argv . It's not part of the C++ standard though, but may still work on some systems.

It's from older UNIX systems, where the environment variables often was passed like this.

The function main may have also a forth argument on Mac OS, of the form char **apple , "containing arbitrary OS-supplied information". See http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B for details.

There are only two forms of main which are required to be supported, and which are portable to all platforms. But an implementation can add any additional forms it wants: int main( double ) would be legal, for example (although I've never heard of an implementation which uses it), as would int main( char const* arg0... ) . In practice, "classical" Unix would support int main( int argc, char** argv, char** environ ) ; this is not required by Posix, and presumably, there are some Unix which don't support it. Outside of the Unix world, many early C implementations tried to look like Unix, and so may also support this (today more for reasons of backwards compatibility than to look like Unix).

It's not something you can count on, however.

You can pass char *env[] , it need not be named like this, though, in order to pass a different set of environment-variables. You can change the environment the particular process is executing in.

See this article, there is an explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM