It is not always. It's the value that you gave the program by the Operation System. For example when starting a program using exec
you can set that to an arbitrary value:
int execve(const char *filename, char *const argv[],
char *const envp[]);
The first parameter is the file to start, and argv will contains argv[0] and all other parameters for main. envp contains the environment variables (not defined by Standard C or C++. This is a posix thing).
More precisely, this is the definition of argv in C++:
An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations
shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
In the latter form argc shall be the number of arguments passed to the program from the environment in
which the program is run. If argc is nonzero these arguments shall be supplied in argv[0]
through
argv[argc-1]
as pointers to the initial characters of null-terminated multibyte strings (NTMBSs)
(17.3.2.1.3.2) and argv[0]
shall be the pointer to the initial character of a NTMBS that represents the
name used to invoke the program or "". The value of argc shall be nonnegative. The value of
argv[argc]
shall be 0. [Note: it is recommended that any further (optional) parameters be added after
argv. ]
It's pretty much up to the implementation what defines a "name used to invoke the program". If you want to get the full path of your executable, you can use GetModuleFileName on Windows, and argv[0]
(for getting the name used to execute, may be relative) together with getcwd
(for getting the current working directory, trying to make the name absolute).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…