[basic.start.main]

# 6 Basics [[basic]](./#basic)

## 6.10 Program execution [[basic.exec]](basic.exec#basic.start.main)

### 6.10.3 Start and termination [[basic.start]](basic.start#main)

#### 6.10.3.1 main function [basic.start.main]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7129)

A program shall contain exactly one function called main that belongs to the global scope[.](#1.sentence-1)

Executing a program starts a main thread of execution ([[intro.multithread]](intro.multithread "6.10.2 Multi-threaded executions and data races"), [[thread.threads]](thread.threads "32.4 Threads"))
in which the main function is invoked[.](#1.sentence-2)

It is implementation-defined
whether a program in a freestanding environment is required to define a main function[.](#1.sentence-3)

[*Note [1](#note-1)*:

In a freestanding environment, startup and termination isimplementation-defined; startup contains the
execution of constructors for non-local objects with static storage duration;
termination contains the execution of destructors for objects with static storage
duration[.](#1.sentence-4)

â *end note*]

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7147)

An implementation shall not predefine the main function[.](#2.sentence-1)

Its type shall have C++ language linkage
and it shall have a declared return type of typeint, but otherwise its type is implementation-defined[.](#2.sentence-2)

An implementation shall allow both

- [(2.1)](#2.1)

a function of () returning int and

- [(2.2)](#2.2)

a function of (int, pointer to pointer to char) returning int

as the type of main ([[dcl.fct]](dcl.fct "9.3.4.6 Functions"))[.](#2.sentence-3)

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[.](#2.sentence-4)

Ifargc is nonzero these arguments shall be supplied inargv[0] through argv[argc - 1] as pointers to the initial
characters of null-terminated multibyte strings (ntmbss) ([[multibyte.strings]](multibyte.strings "16.3.3.3.4.3 Multibyte strings"))
and argv[0] shall be the pointer to
the initial character of an ntmbs that represents the name used to
invoke the program or ""[.](#2.sentence-5)

The value of argc shall be
non-negative[.](#2.sentence-6)

The value of argv[argc] shall be 0[.](#2.sentence-7)

*Recommended practice*: Any further (optional) parameters should be added after argv[.](#2.sentence-8)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7179)

The function main shall not be named by an expression[.](#3.sentence-1)

The linkage ([[basic.link]](basic.link "6.7 Program and linkage")) of main isimplementation-defined[.](#3.sentence-2)

A program that defines main as
deleted or that declares main to beinline, static, constexpr, or consteval is ill-formed[.](#3.sentence-3)

The function main shall not be a coroutine ([[dcl.fct.def.coroutine]](dcl.fct.def.coroutine "9.6.4 Coroutine definitions"))[.](#3.sentence-4)

The main function shall not be declared with a[*linkage-specification*](dcl.link#nt:linkage-specification "9.12 Linkage specifications [dcl.link]") ([[dcl.link]](dcl.link "9.12 Linkage specifications")) other than "C++"[.](#3.sentence-5)

A program that declares

- [(3.1)](#3.1)

a variable main that belongs to the global scope, or

- [(3.2)](#3.2)

a function main that belongs to the global scope and
is attached to a named module, or

- [(3.3)](#3.3)

a function template main that belongs to the global scope, or

- [(3.4)](#3.4)

an entity named main with C language linkage (in any namespace)

is ill-formed[.](#3.sentence-6)

The name main is
not otherwise reserved[.](#3.sentence-7)

[*Example [1](#example-1)*:

Member functions, classes, and
enumerations can be called main, as can entities in other
namespaces[.](#3.sentence-8)

â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7211)

Terminating the program
without leaving the current block (e.g., by calling the functionstd​::​exit(int) ([[support.start.term]](support.start.term "17.5 Startup and termination"))) does not destroy any
objects with automatic storage duration ([[class.dtor]](class.dtor "11.4.7 Destructors"))[.](#4.sentence-1)

Ifstd​::​exit is invoked during the destruction of
an object with static or thread storage duration, the program has undefined
behavior[.](#4.sentence-2)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/basic.tex#L7223)

A return statement ([[stmt.return]](stmt.return "8.8.4 The return statement")) in main has the effect of leaving the main function (destroying any objects with automatic storage duration
and evaluating any postcondition assertions of main)
and calling std​::​exit with the return value as the argument[.](#5.sentence-1)

If control flows off the end of
the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of main,
the effect is equivalent to a return with operand 0 (see also [[except.handle]](except.handle "14.4 Handling an exception"))[.](#5.sentence-2)
