自变量说明Argument Description

main 和 wmain 函数中的 argc 形参是一个整数,用来指定从命令行传递到程序的实参的数量。The argc parameter in the main and wmain functions is an integer specifying how many arguments are passed to the program from the command line. 由于程序名被视为实参,因此 argc 的值至少有一个。Since the program name is considered an argument, the value of argc is at least one.

备注Remarks

argv 形参是一个指针数组,这些指针指向表示程序实参的以 null 结尾的字符串。The argv parameter is an array of pointers to null-terminated strings representing the program arguments. 该数组的每个元素指向传递给 main(或 wmain)的参数的字符串表示形式。Each element of the array points to a string representation of an argument passed to main (or wmain). (有关数组的信息,请参阅数组声明。)argv 参数可以声明为指向类型 char (char *argv[]) 的指针数组,或者声明为一个指针(指向指向类型 char (char **argv) 的多个指针)。(For information about arrays, see Array Declarations.) The argv parameter can be declared either as an array of pointers to type char (char *argv[]) or as a pointer to pointers to type char (char **argv). 对于 wmain,argv 参数可以声明为指向类型 wchar_t (wchar_t *argv[]) 的指针数组,或者声明为一个指针(指向类型 wchar_t (wchar_t **argv) 的多个指针)。For wmain, the argv parameter can be declared either as an array of pointers to type wchar_t (wchar_t *argv[]) or as a pointer to pointers to type wchar_t (wchar_t **argv).

按照约定,argv[0] 是用于调用程序的命令。By convention, argv[0] is the command with which the program is invoked. 但是,可以使用 CreateProcess 来生成进程,并且如果同时使用了第一个和第二个参数(lpApplicationNamelpCommandLine),argv[0] 可能不是可执行名称;请使用 GetModuleFileName 来检索可执行名称。However, it is possible to spawn a process using CreateProcess and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name; use GetModuleFileName to retrieve the executable name.

最后一个指针 (argv[argc]) 是 NULL。The last pointer (argv[argc]) is NULL. (有关获取环境变量信息的替代方法,请参阅《运行时库参考》中的 getenv。)(See getenv in the Run-Time Library Reference for an alternative method for getting environment variable information.)

Microsoft 专用Microsoft Specific

envp 参数是以 null 结尾的字符串的数组,这些字符串表示在用户的环境变量中设置的值。The envp parameter is a pointer to an array of null-terminated strings that represent the values set in the user's environment variables. envp 参数可以声明为 char (char *envp[]) 的指针数组,或者声明为一个指针(指向指向 char (char **envp) 的多个指针)。The envp parameter can be declared as an array of pointers to char (char *envp[]) or as a pointer to pointers to char (char **envp). 在 wmain 函数中,envp 参数可以声明为指向 wchar_t (wchar_t *envp[]) 的指针数组,或者声明为一个指针(指向 wchar_t (wchar_t **envp) 的多个指针)。In a wmain function, the envp parameter can be declared as an array of pointers to wchar_t (wchar_t *envp[]) or as a pointer to pointers to wchar_t (wchar_t **envp). 数组的末尾由 NULL * 指针来指示。The end of the array is indicated by a NULL *pointer. 请注意,传递给 main 或 wmain 的环境块是当前环境的“冻结”副本。Note that the environment block passed to main or wmain is a "frozen" copy of the current environment. 如果随后通过调用 putenv 或 _wputenv 更改环境,则当前环境(由 getenv/_wgetenv 以及 _environ_wenviron 变量返回)将发生更改,但 envp 指向的块将不会更改。If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ or _wenviron variables) will change, but the block pointed to by envp will not change. envp 参数在 C 中是与 ANSI 兼容的,但在 C++ 中却不是如此。The envp parameter is ANSI compatible in C, but not in C++.

结束 Microsoft 专用END Microsoft Specific

请参阅See Also

main 函数和程序执行main Function and Program Execution