Main function

A program shall contain a global function named main, which is the designated start of the program in hosted environment. It shall have one of the following forms:

# Notes

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:

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.

# Example

#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
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
CWG 1003C++98supported parameter names of main were overly restrictedall valid parameternames are supported
CWG 1886C++98the main function could be declared with a language linkageprohibited
CWG 2479C++20the main function could be declared constevalprohibited
CWG 2811C++98whether the main function is used after N3214 was unclearit is considered used when named

# See also