main() 是否可以接受任何其他参数?
Are there any other arguments that main() can accept?
我最近在搜索 C 中的环境变量时遇到了以下问题:
int main (int argc, char *argv[], *char *envp[])
我四处搜索,找不到关于我的问题的任何结论。
main()
可以接受的所有可用参数是什么?
替代方案是宽字符版本:
int main(int argc, wchar_t* argv[], wchar_t* envp[])
main
函数在the language specification as the following, no other function signature is provided besides a get-out clause for implementation-specific entrypoint functions (like Apple's 3rd apple
parameter) or Microsoft's WinMain
函数中指定。
5.1.2.2.1 Program startup
The function called at program startup is named main
. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc
and argv
, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent or in some other implementation-defined manner.
If they are declared, the parameters to the main function shall obey the following constraints:
- The value of
argc
shall be nonnegative.
argv[argc]
shall be a null pointer.
- If the value of
argc
is greater than zero, the array members argv[0]
through
argv[argc-1]
inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
- If the value of argc is greater than zero, the string pointed to by
argv[0]
represents the program name argv[0][0]
shall be the null character if the
program name is not available from the host environment. If the value of argc
is
greater than one, the strings pointed to by argv[1]
through argv[argc-1]
represent the program parameters.
- The parameters
argc
and argv
and the strings pointed to by the argv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
C99 和 C11 草案标准允许实现定义的参数集 main
,这些参数将特定于那些系统(不可移植) .来自 5.1.2.2.1
部分:
[...]or in some other implementation-defined manner[...]
我能找到的唯一附加参数是 envp
和 apple
,我们可以在 Wikipedia's C and C++ section on Entry Points:
中找到很好的描述
Other platform-dependent formats are also allowed by the C and C++
standards, except that in C++ the return type must always be int;[6]
for example, Unix (though not POSIX.1) and Microsoft Windows have a
third argument giving the program's environment, otherwise accessible
through getenv in stdlib.h:
int main(int argc, char **argv, char **envp);
Mac OS X and Darwin have a fourth parameter containing arbitrary
OS-supplied information, such as the path to the executing binary:[7]
int main(int argc, char **argv, char **envp, char **apple);
看起来 Windows 有一个 Microsoft specific wmain 需要 wchar_t
:
int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);
我最近在搜索 C 中的环境变量时遇到了以下问题:
int main (int argc, char *argv[], *char *envp[])
我四处搜索,找不到关于我的问题的任何结论。
main()
可以接受的所有可用参数是什么?
替代方案是宽字符版本:
int main(int argc, wchar_t* argv[], wchar_t* envp[])
main
函数在the language specification as the following, no other function signature is provided besides a get-out clause for implementation-specific entrypoint functions (like Apple's 3rd apple
parameter) or Microsoft's WinMain
函数中指定。
5.1.2.2.1 Program startup
The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent or in some other implementation-defined manner. If they are declared, the parameters to the main function shall obey the following constraints:
- The value of
argc
shall be nonnegative.argv[argc]
shall be a null pointer.- If the value of
argc
is greater than zero, the array membersargv[0]
throughargv[argc-1]
inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.- If the value of argc is greater than zero, the string pointed to by
argv[0]
represents the program nameargv[0][0]
shall be the null character if the program name is not available from the host environment. If the value ofargc
is greater than one, the strings pointed to byargv[1]
throughargv[argc-1]
represent the program parameters.- The parameters
argc
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
C99 和 C11 草案标准允许实现定义的参数集 main
,这些参数将特定于那些系统(不可移植) .来自 5.1.2.2.1
部分:
[...]or in some other implementation-defined manner[...]
我能找到的唯一附加参数是 envp
和 apple
,我们可以在 Wikipedia's C and C++ section on Entry Points:
Other platform-dependent formats are also allowed by the C and C++ standards, except that in C++ the return type must always be int;[6] for example, Unix (though not POSIX.1) and Microsoft Windows have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:
int main(int argc, char **argv, char **envp);
Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:[7]
int main(int argc, char **argv, char **envp, char **apple);
看起来 Windows 有一个 Microsoft specific wmain 需要 wchar_t
:
int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);