What is the proper signature of the main
function in C++? What is the correct return type, and what does it mean to return a value from main
? What are the allowed parameter types, and what are their meanings?
Is this system-specific? Have those rules changed over time? What happens if I violate them?
Details on return values and their meaning
Per 3.6.1 (
[basic.start.main]
):The behavior of
std::exit
is detailed in section 18.5 ([support.start.term]
), and describes the status code:From Standard docs., 3.6.1.2 Main Function,
int main() { / ... / }
andint main(int argc, char* argv[]) { / ... / }
Hope that helps..
The exact wording of the latest published standard (C++14) is:
This makes it clear that alternative spellings are permitted so long as the type of
main
is the typeint()
orint(int, char**)
. So the following are also permitted:int main(void)
auto main() -> int
int main ( )
signed int main()
typedef char **a; typedef int b, e; e main(b d, a c)
The two valid mains are
int main()
andint main(int, char*[])
. Any thing else may or may not compile. Ifmain
doesn't explicitly return a value, 0 is implicitly returned.The
main
function must be declared as a non-member function in the global namespace. This means that it cannot be a static or non-static member function of a class, nor can it be placed in a namespace (even the unnamed namespace).The name
main
is not reserved in C++ except as a function in the global namespace. You are free to declare other entities namedmain
, including among other things, classes, variables, enumerations, member functions, and non-member functions not in the global namespace.You can declare a function named
main
as a member function or in a namespace, but such a function would not be themain
function that designates where the program starts.The
main
function cannot be declared asstatic
orinline
. It also cannot be overloaded; there can be only one function namedmain
in the global namespace.The
main
function cannot be used in your program: you are not allowed to call themain
function from anywhere in your code, nor are you allowed to take its address.The return type of
main
must beint
. No other return type is allowed (this rule is in bold because it is very common to see incorrect programs that declaremain
with a return type ofvoid
; this is probably the most frequently violated rule concerning themain
function).There are two declarations of
main
that must be allowed:In (1), there are no parameters.
In (2), there are two parameters and they are conventionally named
argc
andargv
, respectively.argv
is a pointer to an array of C strings representing the arguments to the program.argc
is the number of arguments in theargv
array.Usually,
argv[0]
contains the name of the program, but this is not always the case.argv[argc]
is guaranteed to be a null pointer.Note that since an array type argument (like
char*[]
) is really just a pointer type argument in disguise, the following two are both valid ways to write (2) and they both mean exactly the same thing:Some implementations may allow other types and numbers of parameters; you'd have to check the documentation of your implementation to see what it supports.
main()
is expected to return zero to indicate success and non-zero to indicate failure. You are not required to explicitly write areturn
statement inmain()
: if you letmain()
return without an explicitreturn
statement, it's the same as if you had writtenreturn 0;
. The following twomain()
functions have the same behavior:There are two macros,
EXIT_SUCCESS
andEXIT_FAILURE
, defined in<cstdlib>
that can also be returned frommain()
to indicate success and failure, respectively.The value returned by
main()
is passed to theexit()
function, which terminates the program.Note that all of this applies only when compiling for a hosted environment (informally, an environment where you have a full standard library and there's an OS running your program). It is also possible to compile a C++ program for a freestanding environment (for example, some types of embedded systems), in which case startup and termination are wholly implementation-defined and a
main()
function may not even be required. If you're writing C++ for a modern desktop OS, though, you're compiling for a hosted environment.