About The main() Function
Go Up to The main() Function Index
Every C and C++ program must have a program-startup function:
- Console-based programs call the
mainfunction at startup and thewmainfunction for UNICODE programs. - Windows GUI programs call the
WinMainfunction at startup and thewWinMainfunction for UNICODE programs.
Where you place the startup function is a matter of preference. Some programmers place main at the beginning of the file, others at the end. Regardless of its location, the following points about main always apply:
- _TCHAR Mapping (for UnicodeString, _tMain and _tWinMain)
- Arguments to main()
- Wildcard arguments
- Using -p (Pascal Calling Conventions)
- The value that main() returns
Our Clang-enhanced C++ Compilers (BCC64, BCCIOSARM, and BCCAARM) now enforce the requirement that declarations have a type in C++. So, for example, you now receive an error if WinMain has no return type, as in:
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
...
}
The above now generates the following error:
- C++ requires a type specifier for all declarations
To remedy this error, update the code to read:
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
...
}