C++Introduction to C++Learn C++

Learn About the Main Function in C++

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;

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,

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)
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 ,

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,

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,

C++ Builder Console Blank Application for Windows,

C++ Builder Console VCL Blank Application for Windows,

C++ Builder Console FMX Blank Application for Windows and other OSes,

C++ Builder VCL Blank Application, Windows Application with Form Units and GUIs (Project1.cpp example),

C++ Builder FMX Blank Application, Multi-Device Application with Form Units and GUIs (Project1.cpp example) ,

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

C++Introduction to C++Learn C++

Learn Classes and Objects in C++

C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes