|
| 1 | +--- |
| 2 | +title: "Startup and Termination (C++)" |
| 3 | +ms.date: "12/10/2019" |
| 4 | +ms.assetid: c6568ee6-40ab-4ae8-aa44-c99e232f64ac |
| 5 | +--- |
| 6 | +# main function and command-line arguments |
| 7 | + |
| 8 | +All C++ programs must have a `main` function. If you try to compile a C++ *.exe* project without a main function, the compiler will raise an error. Dynamic-link libraries and static libraries don't have a `main` function. Although `main` is the starting point of execution, but before a program enters the `main` function, all static class members without explicit initializers are set to zero. In Microsoft C++, global static objects are also initialized before entry to `main`. Several restrictions apply to the `main` function that do not apply to any other C++ functions. The `main` function: |
| 9 | + |
| 10 | +- Cannot be overloaded (see [Function Overloading](function-overloading.md)). |
| 11 | +- Cannot be declared as **inline**. |
| 12 | +- Cannot be declared as **static**. |
| 13 | +- Cannot have its address taken. |
| 14 | +- Cannot be called. |
| 15 | + |
| 16 | +If your source files use Unicode wide characters, you can use `wmain`, which is the wide-character version of `main`. |
| 17 | + |
| 18 | +The `main` function is not predefined by the compiler. It must be supplied in the program text. |
| 19 | + |
| 20 | +The declaration syntax for `main` is as follows: |
| 21 | + |
| 22 | +```cpp |
| 23 | +int main(); |
| 24 | +int main(int argc, char *argv[], char *envp[]); |
| 25 | +``` |
| 26 | +
|
| 27 | +**Microsoft Specific** |
| 28 | +
|
| 29 | +The declaration syntax for `wmain` is as follows: |
| 30 | +
|
| 31 | +```cpp |
| 32 | +int wmain( ); |
| 33 | +int wmain(int argc, wchar_t *argv[], wchar_t *envp[]); |
| 34 | +``` |
| 35 | + |
| 36 | +You can also use `_tmain`, which is defined in tchar.h. `_tmain` resolves to `main` unless _UNICODE is defined. In that case, `_tmain` resolves to `wmain`. |
| 37 | + |
| 38 | +If no return value is specified, the compiler supplies a return value of zero. Alternatively, the `main` and `wmain` functions can be declared as returning **void** (no return value). If you declare `main` or `wmain` as returning **void**, you cannot return an exit code to the parent process or operating system by using a [return](../cpp/return-statement-in-program-termination-cpp.md) statement. To return an exit code when `main` or `wmain` is declared as **void**, you must use the [exit](../cpp/exit-function.md) function. |
| 39 | + |
| 40 | +**END Microsoft Specific** |
| 41 | + |
| 42 | +The types for `argc` and `argv` are defined by the language. The names `argc`, `argv`, and `envp` are traditional, but are not required by the compiler. For more information and an example, see [Argument Definitions](../cpp/argument-definitions.md). |
| 43 | + |
| 44 | +## Command line arguments |
| 45 | + |
| 46 | +The arguments in the prototype |
| 47 | + |
| 48 | +```cpp |
| 49 | +int main( int argc, char* argv[], char* envp[]); |
| 50 | +int wmain( int argc, wchar_t* argv[], wchar_t* envp[]); |
| 51 | +``` |
| 52 | +
|
| 53 | +allow convenient command-line parsing of arguments and, optionally, access to environment variables. The argument definitions are as follows: |
| 54 | +
|
| 55 | +*argc*<br/> |
| 56 | +An integer that contains the count of arguments that follow in *argv*. The *argc* parameter is always greater than or equal to 1. |
| 57 | +
|
| 58 | +*argv*<br/> |
| 59 | +An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, `argv[0]` is the command with which the program is invoked, `argv[1]` is the first command-line argument, and so on, until `argv[argc]`, which is always NULL. See [Customizing Command Line Processing](../cpp/customizing-cpp-command-line-processing.md) for information on suppressing command-line processing. |
| 60 | +
|
| 61 | +The first command-line argument is always `argv[1]` and the last one is `argv[argc - 1]`. |
| 62 | +
|
| 63 | +> [!NOTE] |
| 64 | +> 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/libloaderapi/nf-libloaderapi-getmodulefilenamew) 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, and its fully-qualified path. |
| 65 | +
|
| 66 | +**Microsoft Specific** |
| 67 | +
|
| 68 | +*envp*<br/> |
| 69 | +The *envp* array, which is a common extension in many UNIX systems, is used in Microsoft C++. It is an array of strings representing the variables set in the user's environment. This array is terminated by a NULL entry. It can be declared as an array of pointers to **char** (`char *envp[]`) or as a pointer to pointers to **char** (`char **envp`). If your program uses `wmain` instead of `main`, use the **wchar_t** data type instead of **char**. The environment block passed to `main` and `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` or `_wgetenv` and the `_environ` or `_wenviron` variable) will change, but the block pointed to by envp will not change. See [Customizing Command Line Processing](../cpp/customizing-cpp-command-line-processing.md) for information on suppressing environment processing. This argument is ANSI compatible in C, but not in C++. |
| 70 | +
|
| 71 | +**END Microsoft Specific** |
| 72 | +
|
| 73 | +## Example |
| 74 | +
|
| 75 | +The following example shows how to use the *argc*, *argv*, and *envp* arguments to `main`: |
| 76 | +
|
| 77 | +```cpp |
| 78 | +// argument_definitions.cpp |
| 79 | +// compile with: /EHsc |
| 80 | +#include <iostream> |
| 81 | +#include <string.h> |
| 82 | +
|
| 83 | +using namespace std; |
| 84 | +int main( int argc, char *argv[], char *envp[] ) { |
| 85 | + int iNumberLines = 0; // Default is no line numbers. |
| 86 | +
|
| 87 | + // If /n is passed to the .exe, display numbered listing |
| 88 | + // of environment variables. |
| 89 | +
|
| 90 | + if ( (argc == 2) && _stricmp( argv[1], "/n" ) == 0 ) |
| 91 | + iNumberLines = 1; |
| 92 | +
|
| 93 | + // Walk through list of strings until a NULL is encountered. |
| 94 | + for( int i = 0; envp[i] != NULL; ++i ) { |
| 95 | + if( iNumberLines ) |
| 96 | + cout << i << ": " << envp[i] << "\n"; |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +# Parsing C++ Command-Line Arguments |
| 102 | + |
| 103 | +**Microsoft Specific** |
| 104 | + |
| 105 | +Microsoft C/C++ startup code uses the following rules when interpreting arguments given on the operating system command line: |
| 106 | + |
| 107 | +- Arguments are delimited by white space, which is either a space or a tab. |
| 108 | + |
| 109 | +- The caret character (^) is not recognized as an escape character or delimiter. The character is handled completely by the command-line parser in the operating system before being passed to the `argv` array in the program. |
| 110 | + |
| 111 | +- A string surrounded by double quotation marks ("*string*") is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. |
| 112 | + |
| 113 | +- A double quotation mark preceded by a backslash (\\") is interpreted as a literal double quotation mark character ("). |
| 114 | + |
| 115 | +- Backslashes are interpreted literally, unless they immediately precede a double quotation mark. |
| 116 | + |
| 117 | +- If an even number of backslashes is followed by a double quotation mark, one backslash is placed in the `argv` array for every pair of backslashes, and the double quotation mark is interpreted as a string delimiter. |
| 118 | + |
| 119 | +- If an odd number of backslashes is followed by a double quotation mark, one backslash is placed in the `argv` array for every pair of backslashes, and the double quotation mark is "escaped" by the remaining backslash, causing a literal double quotation mark (") to be placed in `argv`. |
| 120 | + |
| 121 | +## Example |
| 122 | + |
| 123 | +The following program demonstrates how command-line arguments are passed: |
| 124 | + |
| 125 | +```cpp |
| 126 | +// command_line_arguments.cpp |
| 127 | +// compile with: /EHsc |
| 128 | +#include <iostream> |
| 129 | + |
| 130 | +using namespace std; |
| 131 | +int main( int argc, // Number of strings in array argv |
| 132 | + char *argv[], // Array of command-line argument strings |
| 133 | + char *envp[] ) // Array of environment variable strings |
| 134 | +{ |
| 135 | + int count; |
| 136 | + |
| 137 | + // Display each command-line argument. |
| 138 | + cout << "\nCommand-line arguments:\n"; |
| 139 | + for( count = 0; count < argc; count++ ) |
| 140 | + cout << " argv[" << count << "] " |
| 141 | + << argv[count] << "\n"; |
| 142 | +} |
| 143 | +``` |
| 144 | +
|
| 145 | +The following table shows example input and expected output, demonstrating the rules in the preceding list. |
| 146 | +
|
| 147 | +### Results of Parsing Command Lines |
| 148 | +
|
| 149 | +|Command-Line Input|argv[1]|argv[2]|argv[3]| |
| 150 | +|-------------------------|---------------|---------------|---------------| |
| 151 | +|`"abc" d e`|`abc`|`d`|`e`| |
| 152 | +|`a\\b d"e f"g h`|`a\\b`|`de fg`|`h`| |
| 153 | +|`a\\\"b c d`|`a\"b`|`c`|`d`| |
| 154 | +|`a\\\\"b c" d e`|`a\\b c`|`d`|`e`| |
| 155 | +
|
| 156 | +**END Microsoft Specific** |
| 157 | +
|
| 158 | +## Wildcard Expansion |
| 159 | +
|
| 160 | +**Microsoft Specific** |
| 161 | +
|
| 162 | +You can use wildcards — the question mark (?) and asterisk (*) — to specify filename and path arguments on the command-line. |
| 163 | +
|
| 164 | +Command-line arguments are handled by a routine called `_setargv` (or `_wsetargv` in the wide-character environment), which by default does not expand wildcards into separate strings in the `argv` string array. For more information on enabling wildcard expansion, refer to [Expanding Wildcard Arguments](../c-language/expanding-wildcard-arguments.md). |
| 165 | +
|
| 166 | +**END Microsoft Specific** |
| 167 | +
|
| 168 | +## Customizing C++ Command-Line Processing |
| 169 | +
|
| 170 | +**Microsoft Specific** |
| 171 | +
|
| 172 | +If your program does not take command-line arguments, you can save a small amount of space by suppressing use of the library routine that performs command-line processing. This routine is called `_setargv` and is described in [Wildcard Expansion](../cpp/wildcard-expansion.md). To suppress its use, define a routine that does nothing in the file containing the `main` function, and name it `_setargv`. The call to `_setargv` is then satisfied by your definition of `_setargv`, and the library version is not loaded. |
| 173 | +
|
| 174 | +Similarly, if you never access the environment table through the `envp` argument, you can provide your own empty routine to be used in place of `_setenvp`, the environment-processing routine. Just as with the `_setargv` function, `_setenvp` must be declared as **extern "C"**. |
| 175 | +
|
| 176 | +Your program might make calls to the `spawn` or `exec` family of routines in the C run-time library. If this is the case, you should not suppress the environment-processing routine, since this routine is used to pass an environment from the parent process to the child process. |
| 177 | +
|
| 178 | +**END Microsoft Specific** |
| 179 | +
|
| 180 | +## See also |
| 181 | +
|
| 182 | +[Basic Concepts](../cpp/basic-concepts-cpp.md) |
0 commit comments