开发者

Why pass the program name as a parameter to main?

If I 开发者_高级运维call:

./program hello world

then:

argc would be 3.
argv[0] would be "./program".
argv[1] would be "hello".
argv[2] would be "world".

What's the purpose of passing "./program" as an argument? In fact, it's not an argument at all!


You can make symbolic links to the same binary. Depending on what link you use, you will get different behaviour. Busybox is an example of this.


One use is so that the application can know how it was invoked (essentially, what its own name is).

Note that what appears in argv[] is implementation-defined. If you use one of the UNIX exec() functions, for instance, the contents can be whatever you like.


...and you can make make a nice help-function that display a help-text when invoked that doesn't need to be updated when the name of the executable changes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜