Skip to content

Commit b3231d5

Browse files
authored
Merge pull request #4549 from corob-msft/docs/corob/cpp-docs-4195
Updates to C main and wmain signatures
2 parents aeec1c4 + ce8e8b0 commit b3231d5

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
---
22
description: "Learn more about: Argument Description"
33
title: "Argument Description"
4-
ms.date: "11/04/2016"
4+
ms.date: 09/28/2022
55
helpviewer_keywords: ["envp argument", "main function, syntax", "arguments [C++], for main function", "argv argument", "argc argument"]
66
ms.assetid: 91c2cbe3-9aca-4277-afa1-6137eb8fb704
77
---
8-
# Argument Description
8+
# Argument description
99

10-
The `argc` parameter in the **main** and **wmain** functions is an integer specifying how many arguments are passed to the program from the command line. Since the program name is considered an argument, the value of `argc` is at least one.
10+
The *`argc`* parameter in the `main` and `wmain` functions is an integer specifying how many arguments are passed to the program from the command line. Since the program name is considered an argument, the value of *`argc`* is at least one.
1111

1212
## Remarks
1313

14-
The `argv` parameter is an array of pointers to null-terminated strings representing the program arguments. Each element of the array points to a string representation of an argument passed to **main** (or **wmain**). (For information about arrays, see [Array Declarations](../c-language/array-declarations.md).) The `argv` parameter can be declared either as an array of pointers to type **`char`** (`char *argv[]`) or as a pointer to pointers to type **`char`** (`char **argv`). For **wmain**, the `argv` parameter can be declared either as an array of pointers to type **`wchar_t`** (`wchar_t *argv[]`) or as a pointer to pointers to type **`wchar_t`** (`wchar_t **argv`).
14+
The *`argv`* parameter is an array of pointers to null-terminated strings representing the program arguments. Each element of the array points to a string representation of an argument passed to `main` (or `wmain`). (For information about arrays, see [Array declarations](../c-language/array-declarations.md).) The *`argv`* parameter can be declared either as an array of pointers to type **`char`** (`char *argv[]`) or as a pointer to pointers to type **`char`** (`char **argv`). For `wmain`, the *`argv`* parameter can be declared either as an array of pointers to type **`wchar_t`** (`wchar_t *argv[]`) or as a pointer to pointers to type **`wchar_t`** (`wchar_t **argv`).
1515

16-
By convention, `argv`**[0]** is the command with which the program is invoked. However, it is possible to spawn a process using [CreateProcess](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) and if you use both the first and second arguments (`lpApplicationName` and `lpCommandLine`), `argv`**[0]** may not be the executable name; use [GetModuleFileName](/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew) to retrieve the executable name.
16+
By convention, `argv[0]` is the command with which the program is invoked. However, it's possible to spawn a process using [`CreateProcess`](/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) and if you use both the first and second arguments (`lpApplicationName` and `lpCommandLine`), `argv[0]` may not be the executable name; use [`GetModuleFileName`](/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew) to retrieve the executable name.
1717

18-
The last pointer (`argv[argc]`) is **NULL**. (See [getenv](../c-runtime-library/reference/getenv-wgetenv.md) in the *Run-Time Library Reference* for an alternative method for getting environment variable information.)
18+
The last pointer (`argv[argc]`) is `NULL`. (See [`getenv`](../c-runtime-library/reference/getenv-wgetenv.md) in the *Run-Time Library Reference* for an alternative method for getting environment variable information.)
1919

2020
**Microsoft Specific**
2121

22-
The `envp` parameter is a pointer to an array of null-terminated strings that represent the values set in the user's environment variables. The `envp` parameter can be declared as an array of pointers to **`char`** (`char *envp[]`) or as a pointer to pointers to **`char`** (`char **envp`). In a **wmain** function, the `envp` parameter can be declared as an array of pointers to **`wchar_t`** (`wchar_t *envp[]`) or as a pointer to pointers to **`wchar_t`** (`wchar_t **envp`). The end of the array is indicated by a **NULL** \*pointer. Note that the environment block passed to **main** or **wmain** is a "frozen" copy of the current environment. If you subsequently change the environment via a call to _**putenv** or `_wputenv`, the current environment (as returned by `getenv`/`_wgetenv` and the `_environ` or `_wenviron` variables) will change, but the block pointed to by `envp` will not change. The `envp` parameter is ANSI compatible in C, but not in C++.
22+
The *`envp`* parameter is a pointer to an array of null-terminated strings that represent the values set in the user's environment variables. The *`envp`* parameter can be declared as an array of pointers to **`char`** (`char *envp[]`) or as a pointer to pointers to **`char`** (`char **envp`). In a `wmain` function, the *`envp`* parameter can be declared as an array of pointers to **`wchar_t`** (`wchar_t *envp[]`) or as a pointer to pointers to **`wchar_t`** (`wchar_t **envp`). The end of the array is indicated by a `NULL*` pointer. The environment block passed to `main` or `wmain` is a "frozen" copy of the current environment. If you later change the environment via a call to `_putenv` or `_wputenv`, the current environment (as returned by `getenv`/`_wgetenv` and the `_environ` or `_wenviron` variables) will change, but the block pointed to by *`envp`* won't change. The *`envp`* parameter is ANSI/ISO C89 compatible in C, but is a Microsoft extension in C++.
2323

2424
**END Microsoft Specific**
2525

2626
## See also
2727

28-
[main Function and Program Execution](../c-language/main-function-and-program-execution.md)
28+
[`main` function and program execution](../c-language/main-function-and-program-execution.md)

docs/c-language/arguments-to-main.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
---
22
description: "Learn more about: Arguments to main"
33
title: "Arguments to main"
4-
ms.date: "11/04/2016"
4+
ms.date: 09/28/2022
55
ms.assetid: 39824fef-05ad-461d-ae82-49447dda8060
66
---
77
# Arguments to main
88

99
**ANSI 2.1.2.2.1** The semantics of the arguments to main
1010

11-
In Microsoft C, the function called at program startup is called **main**. There is no prototype declared for **main**, and it can be defined with zero, two, or three parameters:
11+
In Microsoft C, the function called at program startup is called **`main`**. There's no prototype declared for **`main`**, and it can be defined with zero, two, or three parameters:
1212

13-
```
13+
```C
1414
int main( void )
1515
int main( int argc, char *argv[] )
1616
int main( int argc, char *argv[], char *envp[] )
1717
```
1818
19-
The third line above, where **main** accepts three parameters, is a Microsoft extension to the ANSI C standard. The third parameter, **envp**, is an array of pointers to environment variables. The **envp** array is terminated by a null pointer. See [The main Function and Program Execution](../c-language/main-function-and-program-execution.md) for more information about **main** and **envp**.
19+
The third line above, where **`main`** accepts three parameters, is a Microsoft extension to the ANSI C standard. The third parameter, *`envp`*, is an array of pointers to environment variables. The *`envp`* array is terminated by a null pointer. For more information about **`main`** and *`envp`*, see [The `main` function and program execution](../c-language/main-function-and-program-execution.md).
2020
21-
The variable **argc** never holds a negative value.
21+
The variable *`argc`* never holds a negative value.
2222
23-
The array of strings ends with **argv[argc]**, which contains a null pointer.
23+
The array of strings ends with `argv[argc]`, which contains a null pointer.
2424
25-
All elements of the **argv** array are pointers to strings.
25+
All elements of the *`argv`* array are pointers to strings.
2626
27-
A program invoked with no command-line arguments will receive a value of one for **argc**, as the name of the executable file is placed in **argv[0]**. (In MS-DOS versions prior to 3.0, the executable-file name is not available. The letter "C" is placed in **argv[0]**.) Strings pointed to by **argv[1]** through **argv[argc - 1]** represent program parameters.
27+
A program invoked with no command-line arguments will receive a value of 1 for *`argc`*, as the name of the executable file is placed in `argv[0]`. (In MS-DOS versions prior to 3.0, the executable-file name isn't available. The letter "C" is placed in `argv[0]`.) Strings pointed to by `argv[1]` through `argv[argc - 1]` represent program parameters.
2828
29-
The parameters **argc** and **argv** are modifiable and retain their last-stored values between program startup and program termination.
29+
The parameters *`argc`* and *`argv`* are modifiable and retain their last-stored values between program startup and program termination.
3030
3131
## See also
3232
Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
11
---
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
55
helpviewer_keywords: ["program startup [C++]", "entry points, program", "main function, program execution", "startup code, main function", "main function", "programs [C++], terminating"]
66
ms.assetid: 5984f1bd-072d-4e06-8640-122fb1454401
77
---
8-
# main Function and Program Execution
8+
# `main` function and program execution
99

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.
1111

12-
## Syntax
12+
Several restrictions apply to the `main` function that don't apply to any other C functions. The `main` function:
1313

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[ ] );
1627
```
1728
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+
1831
## Remarks
1932
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.
2138
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).
2340
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
2542
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).
2744
2845
## See also
2946
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

Comments
 (0)