Share via


main, wmain

main( int argc**,char *argv[ ],char *envp[ ] )**
{
program-statements
}

wmain( int argc**, wchar_t *argv[ ], wchar_t *envp[ ] )**
{
program-statements
}

The main function marks the beginning and end of program execution. A C or C++ program must have one function named main. If your code adheres to the Unicode programming model, you can use the wide-character version of main, which is wmain.

The main and wmain functions can take the following three optional arguments, traditionally called argc, argv, and envp (in that order):

argc

An integer specifying how many arguments are passed to the program from the command line. Because the program name is considered an argument, argc is at least 1.

argv

An array of null-terminated strings. It can be declared as an array of pointers to char(char *argv[ ] or wchar_t *argv[ ] for wmain) or as a pointer to pointers to char(char **argv or wchar_t **argv for wmain). The first string (argv[0]) is the program name, and each following string is an argument passed to the program from the command line. The last pointer (argv[argc]) is NULL.

Microsoft Specific —>

envp

A pointer to an array of environment strings. 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 end of the array is indicated by a NULL pointer. 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/_wgetenv** and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change. This argument is ANSI compatible in C, but not in C++.

END Microsoft Specific