The Main Function is the main part of an application coded in C++, it is the designated entry point to a program that is executed in an operating system (on Windows, iOS, Mac-OS, Android, Linux etc.). Every C++ application program has a main function which starts all application statements, and functions. We can create C++ files without the main function and we can use them as a library, dynamic or static library, or sub c++ files added to your main c++ file but we can not run them alone without the main function.
In some C and C++ compilers the main() function can be used without any parameters and without a return, as below;
1 2 3 4 5 6 |
main() { // main program here } |
In general a main() function returns an integer value that represents the program ended successfully. This is necessary in some cases to be sure that our application or code worked well without any problems, crashes, etc. This return allows us to use some other functions safely in other applications. In the whole application run time there is a catch and if the execution of main
ends normally without encountering a return
statement the compiler assumes the function ends with an implicit return statement
This is the most general main() function example without parameters,
1 2 3 4 5 6 7 |
int main() { // main program here return 0; } |
Here return value can be EXIT_SUCCESS to represent the application run and exits successfully or EXIT_FAILURE to represent that the applications has been failed, 0 is also used as a successful return (same as EXIT_SUCCESS). In general we use return 0; We can brief this as in this table,
The application runs and exits successfully The application runs and exits successfully (defined in <cstdlib> header) | 0 EXIT_SUCCESS |
The application failed (defined in <cstdlib> header) | EXIT_FAILURE |
When the main() function returns zero in a implicit way or explicit way, it is interpreted by the operating system as that the program ended successfully. Return values of other functions can be used at the end of return of main
, and some operating systems may give access to that value to the caller in some way while this is not required nor necessarily portable between system platforms.
Generally the main function is used with two parameters ( argc and argv), with arguments. These parameters allow arbitrary multibyte character strings to be passed from the execution environment (operating system).
In general main function used as below with arguments ,
1 2 3 4 5 6 7 |
int main (int argc, char *argv[]) { // main program here return 0; } |
Here;
– argc is an argument count, a non-negative value representing the number of arguments passed to the program from the operating systems environment. If argc > 0, there must be parameters in argv[].
– argv[] is a multibyte array that represents the arguments passed to the program from the execution. argv[0] is not a null pointer it points to a string that represents the name used to invoke the program, or to an empty string. This parameter is a string and it can be used with std::strtok to delimit it in tokens. Number of the elements of the array pointed to by argv
should be argc+1
, and the last element ( argv[argc]
) is guaranteed to be a null pointer.
In C++ Builder, the main() function is defined as below,
1 2 3 4 5 6 |
int _tmain(int argc, _TCHAR* argv[]) { return 0; } |
In general, the main() function,
– cannot be used anywhere in the programs because it is the main program and this means it cannot be called recursively too
– it doesn’t need to contain the return statement, it returns 0 if the application control reaches the end of main() without encountering a return statement,
– it’s address cannot be used
– It cannot be predefined and cannot be overloaded
– the return type of the main function cannot be deduced like using auto as a return type (since C++14)
– It cannot be defined as deleted or declared with C language linkage (since C++17)
– Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program)
Here are some main() function examples in different Blank C++ Applications.
General C++ Code,
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main (int argc, char *argv[]) { // main program here return 0; } |
C++ Builder Console Blank Application for Windows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#pragma hdrstop #pragma argsused #include <stdio.h> #ifdef _WIN32 #include <tchar.h> #else typedef char _TCHAR; #define _tmain main #endif int _tmain(int argc, _TCHAR* argv[]) { return 0; } |
C++ Builder Console VCL Blank Application for Windows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <vcl.h> #include <windows.h> #pragma hdrstop #pragma argsused #include <tchar.h> #include <stdio.h> int _tmain(int argc, _TCHAR* argv[]) { return 0; } |
C++ Builder Console FMX Blank Application for Windows and other OSes,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <fmx.h> #pragma hdrstop #pragma argsused #ifdef _WIN32 #include <tchar.h> #else typedef char _TCHAR; #define _tmain main #endif #include <stdio.h> int _tmain(int argc, _TCHAR* argv[]) { return 0; } |
C++ Builder VCL Blank Application, Windows Application with Form Units and GUIs (Project1.cpp example),
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include <tchar.h> //--------------------------------------------------------------------------- USEFORM("Unit1.cpp", Form1); //--------------------------------------------------------------------------- int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { try { Application->Initialize(); Application->MainFormOnTaskBar = true; Application->CreateForm(__classid(TForm1), &Form1); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //--------------------------------------------------------------------------- |
C++ Builder FMX Blank Application, Multi-Device Application with Form Units and GUIs (Project1.cpp example) ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
//--------------------------------------------------------------------------- #include <fmx.h> #ifdef _WIN32 #include <tchar.h> #endif #pragma hdrstop #include <System.StartUpCopy.hpp> //--------------------------------------------------------------------------- USEFORM("Unit1.cpp", Form1); //--------------------------------------------------------------------------- extern "C" int FMXmain() { try { Application->Initialize(); Application->CreateForm(__classid(TForm1), &Form1); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //--------------------------------------------------------------------------- |