Which Is The Only Function All C++ Programs Must Contain?

main ( ) in C++ language is the only function that can be defined once in a program. Each program written in C++ language has a definition of the main ( ) function. This is because the grammar of the C++ language is written in such a way that it allows only the main ( ) function in the program. The main ( ) function can be defined with arguments and without arguments.

main ( ) function without argument:

         int main (

          {

          }

mian ( ) function with argument:

int main(int argc, char *argv[ ])

          {

          } 

argc –  denotes the number of arguments passed to the main ( ) function. 

argv –  denotes a pointer that points to the first array element of argc+1 pointers, the last element of the array of argc+1 is null. If argv[0] is non-null, it contains the name of the program.  argv[0] is empty if the host environment does not reflect the name of the program.

argc is the argument count, and argv is the argument vector. The names of the arguments in function main ( ) are not keywords; argc and argv are not keywords, so the programmer can change the name of argc and argv. Thus main ( ) function with the following declaration is equally valid: 

int main(int acc, char** avv)

The return value from the main function indicates the termination status of the program. Successful termination is indicated by 0, and the non-zero value indicates that the program terminated in failure.

The main( ) function marks the beginning of the program. The call to the main ( ) function is made when all the static and non-static objects are initialized. Thus, the main ( ) function is the predefined entry point of the program.

The main ( ) function has two parameters that can accept multibyte character strings passed using the command prompt. These parameters are also known as command-line arguments. The pointers arg[1] .. argv[argc-1] points to the first character of the passed string. argv[0] is the first character of the string which is null-terminated. This string beginning at argv[0] is used to call the program and begin program execution.

The argv array size is argc+1, and the end of the array is marked by argv[argc]. The value at argv[argc] is a null pointer. The properties possessed by the main ( ) function is as follows:

  •       The programmer can not specify the prototype of the program.
  •       The main ( ) function returns an integer value; the execution of the return statement is the same as the execution of the exit statement. On the execution of the return statement, the language compiler flushes and terminates all open connections of the streams and discards the temporary file created to execute the program. When this process is done, control is returned to the execution environment.
  •       If the return type of the main( ) function is not defined and the program terminates without executing the return statement, then the termination status of the executing program is undefined. On the other hand, if the program contains a main ( ) function definition having a return type integer and the executing program terminates without executing the return statement, then also control flushes and terminates all open connections of the streams and discards the temporary file created to execute the program. When this process is done, control is returned to the execution environment. This is because the compiler considers the closing curly bracket of the main function to be a return statement.
  • It is often considered that the main ( ) function marks the beginning of the execution to be more precise. The first function to be executed is _start( ). The _start ( )  function is given by the C++ runtime routine. This _start () function is automatically linked to the C++ program when it is compiled. The execution of the main ( ) function highly relies on the operating system to get executed.

Not only C++, but the Unix program also expects a return statement at the end of the program. The value returned at the end of the program is 0 on successful execution of the program and -1 when program execution is unsuccessful.

Structure of C++ program

/* 1  # include */
/* 2  defines macros */
/* 3  external linking/declarations */
/* 4  typedefs */
/* 5  declaration of global variables  */
/* 6 prototype of the function */
int main(int argc, char *argv[ ])
{
	/*7 program instructions */
}
/* function decalration */
  1. # includes<name_of_the_directory>

#include is a preprocessor directory that makes the compiler to include a referenced file. Header files in C++ program have .h extension and do not contain executable code.

  1. defines macros

This is used to specify macros. Macros are defined by all capital letters; capital letters are used to distinguish macro from variables and function names.

  1. External linking/declaration

This section declares variables that add the declared variable to the compilation file and makes it available to the compiled program.

  1. typedefs

typedefs are used for structures declaration, unions, and enumerations.

  1. Global variable decelerations

Global variables are declared to be used in the main ( ) function, and all the other functions declared in the program. The value of global variables persists throughout the programs.

  1. Prototype of functions

A function prototype is declared before main ( ). It specifies which functions will be used in the program. Function definition is given after main ( ) function.

  1. Program Instructions       

Program instructions are written to achieve the desired objective. All the variables and function calls are written within the main ( ) function. The program logic is written using programming instructions, and these instructions build main ( ).

  1. Definition of function

Functions are used to attain the power of reusability. The declared function can be called from main( ) as well as from other functions. 


When the C++ program is executed, the operating system searches the beginning of the program from where the program execution should start. The search operating system ends at main ( ). The Operating System maintains an address table that marks the entry of the program with the address of the main ( ) function. All the functions which are declared in the program are associated with the main function. The Operating System links the address of all the functions with the address of the main ( ) function. Thus it makes an extra entry in the address table that contains a pointer to associated functions.

If there is no main, then there will be no entry in the address table maintained by the operating system. If there will be no entry of the main ( ) function in the address table, then there will be no entry of the associated function in the address table maintained by the operating system. This will halt program execution, and the objective of writing a program will not be achieved.