6.9.3.1 main function [basic.start.main]
Executing a program starts a main thread of execution ([intro.multithread], [thread.threads])
in which the main function is invoked.
It is implementation-defined
whether a program in a freestanding environment is required to define a main
function.
An implementation shall not predefine the main function.
Its type shall have C++ language linkage
and it shall have a declared return type of type
int, but otherwise its type is implementation-defined.
An implementation shall allow both
- a function of () returning int and
- a function of (int, pointer to pointer to char) returning int
In the latter form, for purposes of exposition, the first function
parameter is called argc and the second function parameter is
called argv, where argc shall be the number of
arguments passed to the program from the environment in which the
program is run.
If
argc is nonzero these arguments shall be supplied in
argv[0] through argv[argc-1] as pointers to the initial
characters of null-terminated multibyte strings (ntmbss) ([multibyte.strings])
and argv[0] shall be the pointer to
the initial character of a ntmbs that represents the name used to
invoke the program or "".
The value of argc shall be
non-negative.
The value of argv[argc] shall be 0.
Recommended practice: Any further (optional) parameters should be added after argv.
The function main shall not be used within
a program.
A program that defines main as
deleted or that declares main to be
inline, static, constexpr, or consteval is ill-formed.
A program that declares
- a variable main that belongs to the global scope, or
- a function main that belongs to the global scope and is attached to a named module, or
- a function template main that belongs to the global scope, or
- an entity named main with C language linkage (in any namespace)
The name main is
not otherwise reserved.
Terminating the program
without leaving the current block (e.g., by calling the function
std::exit(int) ([support.start.term])) does not destroy any
objects with automatic storage duration ([class.dtor]).
If
std::exit is invoked during the destruction of
an object with static or thread storage duration, the program has undefined
behavior.
A return statement ([stmt.return]) in main has the effect of leaving the main
function (destroying any objects with automatic storage duration) and
calling std::exit with the return value as the argument.
If control flows off the end of
the compound-statement of main,
the effect is equivalent to a return with operand 0
(see also [except.handle]).