You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/cpp/language/basic_concepts/main_function.mdx
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ The C++ standard recommends implementation-defined main functions to place the e
54
54
Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.
55
55
</ParamDoc>
56
56
<ParamDocname="argv">
57
-
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 <Missing>null-terminated multibyte strings</Missing> that represent the arguments passed to the program from the execution environment. If `argv[0]` is not a null pointer (or, equivalently, if `argc > 0`), it points to a string that represents the name used to invoke the program, or to an empty string.
57
+
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 <DocLinkdest="/cpp/library/text/multibyte">null-terminated multibyte strings</DocLink> that represent the arguments passed to the program from the execution environment. If `argv[0]` is not a null pointer (or, equivalently, if `argc > 0`), it points to a string that represents the name used to invoke the program, or to an empty string.
58
58
</ParamDoc>
59
59
<ParamDocname="body">
60
60
The body of the `main` function.
@@ -63,30 +63,30 @@ The C++ standard recommends implementation-defined main functions to place the e
63
63
64
64
## Explanation
65
65
66
-
The `main` function is called at program startup after <Missing>initialization</Missing> of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in <Missing>hosted</Missing> environment (that is, with an operating system). The entry points to <Missing>freestanding</Missing> programs (boot loaders, OS kernels, etc) are implementation-defined.
66
+
The `main` function is called at program startup after <DocLinkdest="/cpp/language/initialization">initialization</DocLink> of the non-local objects with static <DocLinkdest="/cpp/language/declarations/storage_duration">storage duration</DocLink>. It is the designated entry point to a program that is executed in <DocLinkdest="/cpp/library/freestanding">hosted</DocLink> environment (that is, with an operating system). The entry points to <DocLinkdest="/cpp/library/freestanding">freestanding</DocLink> programs (boot loaders, OS kernels, etc) are <Behaviortype="impl-def">implementation-defined</Behavior>.
67
67
68
-
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 <Missing>`std::strtok`</Missing>. 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.
68
+
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 <DocLinkdest="/cpp/library/text/byte/strtok">`std::strtok`</DocLink>. 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.
69
69
70
70
The `main` function has the following several special properties:
71
71
72
-
- The body of the `main` function does not need to contain the <Missing>`return` statement</Missing>: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`.
73
-
- 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<Revisionsince="C++26">and evaluates any <Missing>postcondition assertions</Missing> of main</Revision>) and then calling <Missing>`std::exit`</Missing> with the same argument as the argument of the return (<Missing>`std::exit`</Missing> then destroys static objects and terminates the program).
72
+
- The body of the `main` function does not need to contain the <DocLinkdest="/cpp/language/statements/return">`return` statement</DocLink>: if control reaches the end of main without encountering a `return` statement, the effect is that of executing `return 0;`.
73
+
- 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<Revisionsince="C++26">and evaluates any <DocLinkdest="/cpp/language/functions/function"section="postcondition-assertions">postcondition assertions</DocLink> of main</Revision>) and then calling <DocLinkdest="/cpp/library/utility/program/exit">`std::exit`</DocLink> with the same argument as the argument of the <DocLinkdest="/cpp/language/statements/return">return</DocLink> (<DocLinkdest="/cpp/library/utility/program/exit">`std::exit`</DocLink> then destroys static objects and terminates the program).
74
74
75
75
The `main` function has several restrictions (violation of which renders the program <Behaviorkind="ill-formed">ill-formed</Behavior>):
76
76
77
-
- It cannot be <Missing>named</Missing> anywhere in the program
77
+
- It cannot be <DocLinkdest="/cpp/language/basic_concepts/definition"section="naming-a-function">named</DocLink> anywhere in the program
78
78
- in particular, it cannot be called recursively
79
79
- its address cannot be taken
80
-
- it cannot be used in a <Missing>`typeid`</Missing> expression<Revisionsince="C++11">or a <Missing>`decltype`</Missing> specifier</Revision>
81
-
- It cannot be predefined and cannot be overloaded: effectively, the name `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 an entity named `main` cannot be declared with C <Missing>language linkage</Missing> in any namespace).
82
-
- It cannot be<Revisionsince="C++11">defined as deleted or</Revision> declared with any language linkage<Revisionsince="C++11">, <Missing>constexpr</Missing></Revision><Revisionsince="C++20">, <Missing>consteval</Missing></Revision>, <Missing>inline</Missing>, or <Missing>static</Missing>.
80
+
- it cannot be used in a <DocLinkdest="/cpp/language/expressions/typeid">`typeid`</DocLink> expression<Revisionsince="C++11">or a <DocLinkdest="/cpp/language/declarations/decltype">`decltype`</DocLink> specifier</Revision>
81
+
- It cannot be predefined and cannot be overloaded: effectively, the name `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 an entity named `main` cannot be declared with C <DocLinkdest="/cpp/language/declarations/language_linkage">language linkage</DocLink> in any namespace).
82
+
- It cannot be<Revisionsince="C++11">defined as deleted or</Revision> declared with any language linkage<Revisionsince="C++11">, <DocLinkdest="/cpp/language/declarations/constexpr">constexpr</DocLink></Revision><Revisionsince="C++20">, <DocLinkdest="/cpp/language/declarations/consteval">consteval</DocLink></Revision>, <DocLinkdest="/cpp/language/declarations/inline">inline</DocLink>, or <DocLinkdest="/cpp/language/classes/static">static</DocLink>.
83
83
- <Revisionsince="C++14">The return type of the `main` function cannot be deduced (`auto main() {...}` is not allowed).</Revision>
84
-
- <Revisionsince="C++20">The `main` function cannot be a <Missing>coroutine</Missing>.</Revision>
85
-
- <Revisionsince="C++20">The `main` function cannot attach to a named <Missing>module</Missing>.</Revision>
84
+
- <Revisionsince="C++20">The `main` function cannot be a <DocLinkdest="/cpp/language/functions/coroutines">coroutine</DocLink>.</Revision>
85
+
- <Revisionsince="C++20">The `main` function cannot attach to a named <DocLinkdest="/cpp/language/basic_concepts/modules">module</DocLink>.</Revision>
86
86
87
87
## Notes
88
88
89
-
If the `main` function is defined with a <Missing>function `try` block</Missing>, the exceptions thrown by the destructors of static objects (which are destroyed by the implied <Missing>`std::exit`</Missing>) are not <Missing>caught</Missing> by it.
89
+
If the `main` function is defined with a <DocLinkdest="/cpp/language/exceptions/try">function `try` block</DocLink>, the exceptions thrown by the destructors of static objects (which are destroyed by the implied <DocLinkdest="/cpp/library/utility/program/exit">`std::exit`</DocLink>) are not <DocLinkdest="/cpp/language/exceptions/catch">caught</DocLink> by it.
90
90
91
91
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:
92
92
@@ -187,6 +187,6 @@ The following behavior-changing defect reports were applied retroactively to pre
187
187
188
188
<DescList>
189
189
<Desc>
190
-
<DocLink slot="item" dest="/c/language/main_function"> C documentation</DocLink> for <span> **main function** </span>
190
+
<DocLink slot="item" dest="/c/language/basic_concepts/main_function"> C documentation</DocLink> for <span> **main function** </span>
0 commit comments