Difference between revisions of "cpp/language/main function"
(Improve some wording, avoiding to call a null pointer a pointer to string) |
(Clarify the description of argv introduced with the previous commit) |
||
Line 18: | Line 18: | ||
{{par begin}} | {{par begin}} | ||
{{par | argc | Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.}} | {{par | argc | Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.}} | ||
− | {{par | argv | Pointer to the first element of an array of {{c|argc + 1}} pointers | + | {{par | argv | Pointer to the first element of an array of {{c|argc + 1}} pointers, of which the last one is null and the previous ones, if any, point to [[cpp/string/multibyte|null-terminated multibyte strings]] that represent the arguments passed to the program from the execution environment. If it is not a null pointer (which happens if and only if {{c|argc}} is zero), {{c|argv[0]}} points to the program name, which can be empty (see below).}} |
{{par | {{spar|body}} | The body of the main function}} | {{par | {{spar|body}} | The body of the main function}} |
Revision as of 14:41, 6 April 2019
A program shall contain a global function named main
, which is the designated start of the program. It shall have one of the following forms:
int main () { body }
|
(1) | ||||||||
int main ( int argc, char *argv[]) { body }
|
(2) | ||||||||
/* another implementation-defined form, with int as return type */ | (3) | ||||||||
argc | - | Non-negative value representing the number of arguments passed to the program from the environment in which the program is run. |
argv | - | Pointer to the first element of an array of argc + 1 pointers, of which the last one is null and the previous ones, if any, point to null-terminated multibyte strings that represent the arguments passed to the program from the execution environment. If it is not a null pointer (which happens if and only if argc is zero), argv[0] points to the program name, which can be empty (see below).
|
body | - | The body of the main function |
The names 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 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.
Explanation
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]
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 several special properties:
main
in 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 a function called "main" cannot be declared with C language linkage in any namespace(since C++17))main
without encountering a return statement, the effect is that of executing return 0;.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:
See also
C documentation for main function
|