Site icon 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;

[crayon-6763b649f21ee679522282/]

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,

[crayon-6763b649f21f5829758599/]

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 ,

[crayon-6763b649f21f7030359511/]

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,

[crayon-6763b649f21f8901169894/]

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,

[crayon-6763b649f21f9957866688/]

C++ Builder Console Blank Application for Windows,

[crayon-6763b649f21fa367351706/]

C++ Builder Console VCL Blank Application for Windows,

[crayon-6763b649f21fb987822744/]

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

[crayon-6763b649f21fe955047971/]

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

[crayon-6763b649f21ff756207486/]

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

[crayon-6763b649f2200979068905/]


Get started building powerful apps with C++Builder!

Exit mobile version