或只是一个共同的约定

或只是一个共同的约定

本文介绍了IS" argv的[0] =&QUOT名称的可执行文件的-;一个公认的标准,或只是一个共同的约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当传递参数的main()在C或C ++应用程序,将的argv [0] 总是可执行文件的名字吗?或者,这仅仅是一个常见的​​约定,并且不保证是真实的100%的时间?

When passing argument to main() in a C or C++ application, will argv[0] always be the name of the executable? Or is this just a common convention and not guaranteed to be true 100% of the time?

推荐答案

臆测(即使是受过教育的猜测)很有趣,但你真的需要去到标准文件,以确保。例如,ISO C11规定(我的重点):

Guesswork (even educated guesswork) is fun but you really need to go to the standards documents to be sure. For example, ISO C11 states (my emphasis):

如果的 ARGC 的值大于零,则指向的字符串的argv [0] 再presents 节目名称; 的argv [0] [0] 应为空字符,如果程序名称不能从主机环境。

所以,不,这只是程序名称,如果该名称是可用。的它的重presents的程序名,不一定的的程序名称。在此之前,本节规定:

So no, it's only the program name if that name is available. And it "represents" the program name, not necessarily is the program name. The section before that states:

如果的 ARGC 的值大于零,阵列成员的argv [0] 的argv [ARGC-1] 包容应包含指向字符串的指针,由主机环境定实现定义的值来编写启动之前。

这是从C99,在previous标准不变,意味着即使的的不被标准的规定 - 它是由完全落实

This is unchanged from C99, the previous standard, and means that even the values are not dictated by the standard - it's up to the implementation entirely.

这意味着程序名称可以是空的,如果主机环境的的提供它,别的,如果主机环境的确实的提供的,规定什么都莫名其妙地重新presents程序名称。在我更残暴的时刻,我会考虑它翻译成斯瓦希里语,以相反的字节顺序运行它通过一个替代密码,然后将它存储: - )

This means that the program name can be empty if the host environment doesn't provide it, and anything else if the host environment does provide it, provided that "anything else" somehow represents the program name. In my more sadistic moments, I would consider translating it into Swahili, running it through a substitution cipher then storing it in reverse byte order :-).

然而,实现定义的确实的有在ISO标准具体含义 - 执行必须记录它是如何工作。因此,即使UNIX,它可以把任何东西它喜欢到的argv [0] EXEC 家庭电话,有(并执行)文件就可以了。

However, implementation-defined does have a specific meaning in the ISO standards - the implementation must document how it works. So even UNIX, which can put anything it likes into argv[0] with the exec family of calls, has to (and does) document it.

这篇关于IS" argv的[0] =&QUOT名称的可执行文件的-;一个公认的标准,或只是一个共同的约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:01