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
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.) The `main` function is where your source code begins 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:
11
11
12
-
-Can't be overloaded (see [Function Overloading](function-overloading.md)).
13
-
-Can't be declared as **inline**.
14
-
-Can't be declared as **static**.
15
-
-Can't have its address taken.
16
-
-Can't be called.
12
+
-Cannot be overloaded (see [Function Overloading](function-overloading.md)).
13
+
-Cannot be declared as **inline**.
14
+
-Cannot be declared as **static**.
15
+
-Cannot have its address taken.
16
+
-Cannot be called.
17
17
18
18
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:
19
19
20
20
```cpp
21
21
intmain();
22
-
intmain(int argc, char *argv[]);
23
22
intmain(int argc, char *argv[], char *envp[]);
24
23
```
25
24
26
25
**Microsoft Specific**
27
26
28
-
If your source files use Unicode wide characters, you can use `wmain`, which is the wide-character version of `main`. The virtual declaration syntax for `wmain` is as follows:
27
+
If your source files use Unicode wide characters, you can use `wmain`, which is the wide-character version of `main`. The declaration syntax for `wmain` is as follows:
29
28
30
29
```cpp
31
30
int wmain( );
32
-
int wmain(int argc, wchar_t *argv[]);
33
31
int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);
34
32
```
35
33
@@ -39,10 +37,15 @@ If no return value is specified, the compiler supplies a return value of zero. A
39
37
40
38
**END Microsoft Specific**
41
39
42
-
## Command-line arguments
40
+
## Commandline arguments
43
41
44
42
The arguments for `main` or `wmain` allow convenient command-line parsing of arguments and, optionally, access to environment variables. The types for `argc` and `argv` are defined by the language. The names `argc`, `argv`, and `envp` are traditional, but you can name them whatever you like.
45
43
44
+
```cpp
45
+
intmain(int argc, char *argv[], char *envp[]);
46
+
int wmain(int argc, wchar_t *argv[], wchar_t *envp[]);
47
+
```
48
+
46
49
The argument definitions are as follows:
47
50
48
51
*argc*<br/>
@@ -154,7 +157,7 @@ The following table shows example input and expected output, demonstrating the r
154
157
155
158
You can use wildcards — the question mark (?) and asterisk (*) — to specify filename and path arguments on the command line.
156
159
157
-
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, see[Expanding Wildcard Arguments](../c-language/expanding-wildcard-arguments.md).
160
+
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).
158
161
159
162
**END Microsoft Specific**
160
163
@@ -164,12 +167,12 @@ Command-line arguments are handled by a routine called `_setargv` (or `_wsetargv
164
167
165
168
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.
166
169
167
-
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. Like the `_setargv` function, `_setenvp` must be declared as **extern "C"**.
170
+
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"**.
168
171
169
172
Your program might make calls to the `spawn` or `exec` family of routines in the C run-time library. If it does, you shouldn't suppress the environment-processing routine, since this routine is used to pass an environment from the parent process to the child process.
0 commit comments