要保留的自变量Arguments to main

ANSI 2.1.2.2.1:main 参数的语义ANSI 2.1.2.2.1 The semantics of the arguments to main

在 Microsoft C 中,程序启动时调用的函数称为 mainIn Microsoft C, the function called at program startup is called main. 没有针对 main 声明的原型,可以用零个、两个或三个参数对其进行定义:There is no prototype declared for main, and it can be defined with zero, two, or three parameters:

int main( void )  
int main( int argc, char *argv[] )  
int main( int argc, char *argv[], char *envp[] )  

上面第三行(其中,main 接受三个参数)是 Microsoft ANSI C 标准扩展。The third line above, where main accepts three parameters, is a Microsoft extension to the ANSI C standard. 第三个参数 (envp) 是指向环境变量的指针的数组。The third parameter, envp, is an array of pointers to environment variables. envp 数组以 null 指针终止。The envp array is terminated by a null pointer. 有关 mainenvp 的详细信息,请参阅 main 函数和程序执行See The main Function and Program Execution for more information about main and envp.

变量 argc 从不保留负值。The variable argc never holds a negative value.

字符串数组以包含 null 指针的 argv[argc] 结束。The array of strings ends with argv[argc], which contains a null pointer.

argv 数组的所有元素都是指向字符串的指针。All elements of the argv array are pointers to strings.

未使用命令行参数调用的程序将接收 argc 的值 1,因为可执行文件的名称将置于 argv[0] 内。A program invoked with no command-line arguments will receive a value of one for argc, as the name of the executable file is placed in argv[0]. (在 3.0 之前的 MS-DOS 版本中,可执行文件名不可用。(In MS-DOS versions prior to 3.0, the executable-file name is not available. 字母“C”将置于 argv[0] 中。)argv[1] 通过 argv[argc – 1] 指向的字符串表示程序参数。The letter "C" is placed in argv[0].) Strings pointed to by argv[1] through argv[argc - 1] represent program parameters.

参数 argargv 是可修改的,并在程序启动与程序终止之间保留最后存储的值。The parameters argc and argv are modifiable and retain their last-stored values between program startup and program termination.

另请参阅See Also

环境Environment