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

每个 C 程序都有必须命名为 main 的主函数。Every C program has a primary (main) function that must be named main. 如果你的代码遵循 Unicode 编程模型,则可以使用 main 的宽字符版本 wmain。If your code adheres to the Unicode programming model, you can use the wide-character version of main, wmain. main 函数充当程序执行的起点。The main function serves as the starting point for program execution. 它通常通过将调用定向到程序中的其他函数来控制程序执行。It usually controls program execution by directing the calls to other functions in the program. 尽管程序可以因为各种原因在程序的其他点上终止,但它通常在 main 的结尾处停止执行。A program usually stops executing at the end of main, although it can terminate at other points in the program for a variety of reasons. 有时,当检测到某一错误时,您可能希望强制终止程序。At times, perhaps when a certain error is detected, you may want to force the termination of a program. 为此,请使用 exit 函数。To do so, use the exit function. 有关使用 exit 函数的信息和示例,请参阅《运行时库参考》。See the Run-Time Library Reference for information on and an example using the exit function.


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


源程序中的函数执行一个或多个特定任务。Functions within the source program perform one or more specific tasks. main 函数可调用这些函数来执行其各自的任务。The main function can call these functions to perform their respective tasks. 当 main 调用另一函数时,它会将执行控制权交给该函数,以便执行在该函数中的第一个语句处开始。When main calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. 当执行 return 语句或到达某个函数的末尾时,该函数会将控制权返回给 main。A function returns control to main when a return statement is executed or when the end of the function is reached.

可以声明任何函数(包括 main)以包含参数。You can declare any function, including main, to have parameters. 术语“参数”或“形参”指的是接收传递到函数的值的标识符。The term "parameter" or "formal parameter" refers to the identifier that receives a value passed to a function. 有关将实参传递到形参的信息,请参阅参数See Parameters for information on passing arguments to parameters. 当一个函数调用另一个函数时,被调用的函数将从实施调用的函数接收其参数的值。When one function calls another, the called function receives values for its parameters from the calling function. 这些值称为“自变量”。These values are called "arguments." 可以将形参声明为 main,以便让它使用以下格式从命令行接收实参:You can declare formal parameters to main so that it can receive arguments from the command line using this format:

在将信息传递给 main 函数时,尽管 C 编译器不需要这些名称,但上述参数在传统上命名为 argcargvWhen you want to pass information to the main function, the parameters are traditionally named argc and argv, although the C compiler does not require these names. argcargv 的类型由 C 语言定义。The types for argc and argv are defined by the C language. 传统上,如果将第三个参数传递给 main,该参数将命名为 envpTraditionally, if a third parameter is passed to main, that parameter is named envp. 本节后面的示例演示如何使用这三个参数访问命令行自变量。Examples later in this section show how to use these three parameters to access command-line arguments. 以下各节说明了这些参数。The following sections explain these parameters.

有关 main 的宽字符版本的说明,请参阅使用 wmainSee Using wmain for a description of the wide-character version of main.

请参阅See Also

main:程序启动main: Program Startup