|
1 | 1 | ---
|
2 |
| -description: "Learn more about: main Function and Program Execution" |
3 |
| -title: "main Function and Program Execution" |
4 |
| -ms.date: "11/04/2016" |
| 2 | +description: "Learn more about: main function and program execution" |
| 3 | +title: "main function and program execution" |
| 4 | +ms.date: 09/28/2022 |
5 | 5 | helpviewer_keywords: ["program startup [C++]", "entry points, program", "main function, program execution", "startup code, main function", "main function", "programs [C++], terminating"]
|
6 | 6 | ms.assetid: 5984f1bd-072d-4e06-8640-122fb1454401
|
7 | 7 | ---
|
8 |
| -# main Function and Program Execution |
| 8 | +# `main` function and program execution |
9 | 9 |
|
10 |
| -Every C program has a primary (main) function that must be named **main**. If your code adheres to the Unicode programming model, you can use the wide-character version of **main**, **wmain**. The **main** function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. A program usually stops executing at the end of **main**, although it can terminate at other points in the program for a variety of reasons. At times, perhaps when a certain error is detected, you may want to force the termination of a program. To do so, use the **exit** function. See the *Run-Time Library Reference* for information on and an example using the [exit](../c-runtime-library/reference/exit-exit-exit.md) function. |
| 10 | +Every C program has a primary function that must be named `main`. The `main` function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program. |
11 | 11 |
|
12 |
| -## Syntax |
| 12 | +Several restrictions apply to the `main` function that don't apply to any other C functions. The `main` function: |
13 | 13 |
|
14 |
| -``` |
15 |
| -main( int argc, char *argv[ ], char *envp[ ] ) |
| 14 | +- Can't be declared as **`inline`**. |
| 15 | +- Can't be declared as **`static`**. |
| 16 | +- Can't have its address taken. |
| 17 | +- Can't be called from your program. |
| 18 | + |
| 19 | +## The `main` function signature |
| 20 | + |
| 21 | +The `main` function doesn't have a declaration, because it's built into the language. If it did, the declaration syntax for `main` would look like this: |
| 22 | + |
| 23 | +```C |
| 24 | +int main( void ); |
| 25 | +int main( int argc, char *argv[ ] ); |
| 26 | +int main( int argc, char *argv[ ], char *envp[ ] ); |
16 | 27 | ```
|
17 | 28 |
|
| 29 | +The `main` function is declared implicitly by using one of these signatures. You may use any of these signatures when you define your `main` function. The Microsoft compiler also allows `main` to have a return type of **`void`** when no value is returned. The *`argv`* and *`envp`* parameters to `wmain` can also be defined as type `char**`. For more information about the arguments, see [Argument description](./argument-description.md). |
| 30 | +
|
18 | 31 | ## Remarks
|
19 | 32 |
|
20 |
| -Functions within the source program perform one or more specific tasks. The **main** function can call these functions to perform their respective tasks. When **main** calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to **main** when a **`return`** statement is executed or when the end of the function is reached. |
| 33 | +Functions within the source program perform one or more specific tasks. The `main` function can call these functions to perform their respective tasks. When `main` calls another function, it passes execution control to the function, so that execution begins at the first statement in the function. A function returns control to `main` when a **`return`** statement is executed or when the end of the function is reached. |
| 34 | +
|
| 35 | +You can declare any function, including `main`, to have parameters. The term "parameter" or "formal parameter" refers to the identifier that receives a value passed to a function. See [Parameters](../c-language/parameters.md) for information on passing arguments to parameters. When one function calls another, the called function receives values for its parameters from the calling function. These values are called *arguments*. You can declare formal parameters to `main` so that it can receive arguments from the command line using the format shown in the function signature. |
| 36 | +
|
| 37 | +When you want to pass information to the `main` function, the parameters are traditionally named *`argc`* and *`argv`*, although the C compiler doesn't require these names. Traditionally, if a third parameter is passed to `main`, that parameter is named *`envp`*. The types for *`argc`*, *`argv`*, and *`envp`* are defined by the C language. You can also declare *`argv`* as `char** argv` and *`envp`* as `char** envp`. Examples later in this section show how to use these three parameters to access command-line arguments. The following sections explain these parameters. |
21 | 38 |
|
22 |
| -You can declare any function, including **main**, to have parameters. The term "parameter" or "formal parameter" refers to the identifier that receives a value passed to a function. See [Parameters](../c-language/parameters.md) for information on passing arguments to parameters. When one function calls another, the called function receives values for its parameters from the calling function. These values are called "arguments." You can declare formal parameters to **main** so that it can receive arguments from the command line using this format: |
| 39 | +If your code adheres to the Unicode programming model, you can use the Microsoft-specific wide-character version of **`main`**, **`wmain`**, as your program's entry point. For more information about this wide-character version of **`main`**, see [Using `wmain`](../c-language/using-wmain.md). |
23 | 40 |
|
24 |
| -When you want to pass information to the **main** function, the parameters are traditionally named `argc` and `argv`, although the C compiler does not require these names. The types for `argc` and `argv` are defined by the C language. Traditionally, if a third parameter is passed to **main**, that parameter is named `envp`. Examples later in this section show how to use these three parameters to access command-line arguments. The following sections explain these parameters. |
| 41 | +## `main` termination |
25 | 42 |
|
26 |
| -See [Using wmain](../c-language/using-wmain.md) for a description of the wide-character version of **main**. |
| 43 | +A program usually stops executing when it returns from or reaches the end of `main`, although it can terminate at other points in the program for various reasons. For example, you may want to force the termination of your program when some error condition is detected. To do so, you can use the `exit` function. For more information on `exit` and an example of usage, see [`exit`](../c-runtime-library/reference/exit-exit-exit.md). |
27 | 44 |
|
28 | 45 | ## See also
|
29 | 46 |
|
30 |
| -[main function and command-line arguments (C++)](../cpp/main-function-command-line-args.md)\ |
31 |
| -[Parsing C Command-Line Arguments](../c-language/parsing-c-command-line-arguments.md) |
| 47 | +[`main` function and command-line arguments (C++)](../cpp/main-function-command-line-args.md)\ |
| 48 | +[Parsing C command-line arguments](../c-language/parsing-c-command-line-arguments.md) |
0 commit comments