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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?