| title | Main function |
|---|
import Code from "@astrojs/starlight/components";
import Behavior from "@components/Behavior.astro"; import { Decl, DeclDoc } from "@components/decl-doc"; import { DR, DRList } from "@components/defect-report"; import { Desc, DescList, DocLink } from '@components/index'; import Missing from "@components/Missing.astro"; import { ParamDoc, ParamDocList } from "@components/param-doc"; import { Revision } from "@components/revision"; import WG21PaperLink from "@components/WG21PaperLink.astro";
A program shall contain a global namespace function named main, which is the designated start of the program in hosted environment. It shall have one of the following forms:
A main function running independently of environment-provided arguments.
A main function accepting environment-provided arguments.
The names of argc and argv are arbitrary, as well as the representation of the types of the parameters: int main(int ac, char** av) is equally valid.
A main function of implement-defined type, returning int.
The C++ standard recommends implementation-defined main functions to place the extra (optional) parameters after argv.
The main function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined.
The parameters of the two-parameter form of the main function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as command line arguments), the pointers [argv[1], argv[argc - 1]] point at the first characters in each of these strings. argv[0] (if non-null) is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment). The strings are modifiable, although these modifications do not propagate back to the execution environment: they can be used, for example, with std::strtok. The size of the array pointed to by argv is at least argc + 1, and the last element, argv[argc], is guaranteed to be a null pointer.
The main function has the following several special properties:
- The body of the
mainfunction does not need to contain thereturnstatement: if control reaches the end of main without encountering areturnstatement, the effect is that of executingreturn 0;. - Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration and evaluates any postcondition assertions of main) and then calling
std::exitwith the same argument as the argument of the return (std::exitthen destroys static objects and terminates the program).
The main function has several restrictions (violation of which renders the program ill-formed):
- It cannot be named anywhere in the program
- in particular, it cannot be called recursively
- its address cannot be taken
- it cannot be used in a
typeidexpression or adecltypespecifier
- It cannot be predefined and cannot be overloaded: effectively, the name
mainin the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that an entity namedmaincannot be declared with C language linkage in any namespace). - It cannot be defined as deleted or declared with any language linkage, constexpr, consteval, inline, or static.
- The return type of the
mainfunction cannot be deduced (auto main() {...}is not allowed). - The
mainfunction cannot be a coroutine. - The
mainfunction cannot attach to a named module.
If the main function is defined with a function try block, the exceptions thrown by the destructors of static objects (which are destroyed by the implied std::exit) are not caught by it.
The manner in which the arguments given at the OS command line are converted into the multibyte character arrays referenced by argv may involve implementation-defined processing:
- MSDN: Parsing C++ Command-Line Arguments
- POSIX: Shell Introduction
A very common implementation-defined form of main() has a third argument (in addition to argc and argv), of type char**, pointing at an array of pointers to the execution environment variables.
Demonstrates how to inform a program about where to find its input and where to write its results. A possible invocation: ./convert table_in.dat table_out.dat
#include <cstdlib>
#include <iomanip>
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "argc == " << argc << '\n';
for (int ndx{}; ndx != argc; ++ndx)
std::cout << "argv[" << ndx << "] == " << std::quoted(argv[ndx]) << '\n';
std::cout << "argv[" << argc << "] == "
<< static_cast<void*>(argv[argc]) << '\n';
/* ... */
return argc == 3 ? EXIT_SUCCESS : EXIT_FAILURE; // optional return value
}Possible output:
argc == 3
argv[0] == "./convert"
argv[1] == "table_in.dat"
argv[2] == "table_out.dat"
argv[3] == 0
- C++23 standard (ISO/IEC 14882:2024)
- 6.9.3.1 main function [basic.start.main]
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
supported parameter names of `main` were overly restricted all valid parameter names are supported the `main` function could be declared with a language linkage prohibited the `main` function could be declared `consteval` prohibited whether the `main` function is used after was unclear it is considered used when named C documentation for **main function**