|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
main: Program Startup
Updated: July 2009
A special function named main is the starting point of execution for all C and C++ programs. If you are writing code that adheres to the Unicode programming model, you can use wmain, which is the wide-character version of main.
The main function is not predefined by the compiler. It must be supplied in the program text.
The declaration syntax for main is
int main(int argc, char *argv, char *envp);
The declaration syntax for wmain is as follows:
int wmain( );
int wmain(int argc, wchar_t *argv, wchar_t *envp);
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.
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 statement. To return an exit code when main or wmain is declared as void, you must use the exit function.
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.