A special function called 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 the wide-character version of main, wmain.
The main function is not predefined by the compiler; rather, it must be supplied in the program text.
The declaration syntax for main is:
int main( );
or, optionally:
int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );
The declaration syntax for wmain is as follows:
int wmain( );
or, optionally:
int wmain( int argc[ , wchar_t *argv[ ] [, wchar_t *envp[ ] ] ] );
You can also use _tmain, which is defined in TCHAR.h. _tmain will resolve to main unless _UNICODE is defined, in which case _tmain will resolve to wmain.
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. See Argument Definitions for more information and for an example.
Alternately, 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 using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function.
Reference
C++ KeywordsUsing wmain Instead of main
main Function Restrictions
int main() { ... }or equivalent. The extra "envp" parameter, the (not very useful) ability to declare argc but not argv, and the ability to return void rather than int are all Microsoft extensions.
int main(int argc, char *argv[]) { ... }
(Note that the value returned is not limited to 0 or 1, and 1 may not be meaningful. The standard return values are 0, EXIT_SUCCESS, and EXIT_FAILURE. Other values are permitted, but will have implementation-specific results.)
This statement is Microsoft Specific but is outside the the Microsoft Specific section markers:
Alternately, 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 using a return statement; to return an exit code when main or wmain is declared as void, you must use the exit function.
ANSI C or C++ requires that main() return an int of 0 or 1 only.
Please note that Visual Studio.NET 2005 reports an error when main() returns void. You have to enable Microsoft Extensions for the error to go away.