The return type of main() function
This question already has an answer here:
The C standard (ISO/IEC 9899:2011) says:
5.1.2.2.1 Program startup
1 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;10) or in some other implementation-defined manner.
10) Thus, int
can be replaced by a typedef name defined as int
, or the type of argv can be written as char **argv
, and so on.
Thus, the only portable declaration for main()
has a return type of int
. If MSVC defines that void
is permitted ('or in some other implementation-defined manner'), so be it, but do not expect the code to be portable. The old versions of the Microsoft compilers (up to and including MSVC 2005) do not permit void main()
: see the documentation at main
: Program startup and The main
Function and Program Execution. However, MSVC 2008 and later are documented to allow void main()
: see main
: Program Startup. The three-argument form of main()
is noted as a common extension in Appendix J:
J.5 Common extensions
The following extensions are widely used in many systems, but are not portable to all implementations. The inclusion of any extension that may cause a strictly conforming program to become invalid renders an implementation nonconforming. Examples of such extensions are new keywords, extra library functions declared in standard headers, or predefined macros with names that do not begin with an underscore.
J.5.1 Environment arguments
In a hosted environment, the main
function receives a third argument, char *envp[]
, that points to a null-terminated array of pointers to char
, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).
The value returned from main()
is transmitted to the 'environment' in an implementation-defined way.
5.1.2.2.3 Program termination
1 If the return type of the main
function is a type compatible with int
, a return from the initial call to the main
function is equivalent to calling the exit
function with the value returned by the main
function as its argument;11) reaching the }
that terminates the main
function returns a value of 0. If the return type is not compatible with int
, the termination status returned to the host environment is unspecified.
11) In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main
will have ended in the former case, even where they would not have in the latter.
Note that 0
is mandated as 'success'. You can use EXIT_FAILURE
and EXIT_SUCCESS
from <stdlib.h>
if you prefer, but 0 is well established, and so is 1. See also Exit codes greater than 255 — possible?.
7.22.4.4 The exit
function
¶5 Finally, control is returned to the host environment. If the value of status
is zero or EXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned. If the value of status
is EXIT_FAILURE
, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
According to c standard main()
should return integer for informing success or failure.Generally for success it returns zero and for failure it returns a integer value(either positive or negative). Generally main is declared as
int main(void);
so it expects integer as return type.
If command line arguments are there,
int main(int argc,char *argv[]);
void main()
是非标准的C, int main()
是标准的。
下一篇: main()函数的返回类型