C++

Use of main() Function in C++

The main() function is the entry point of a program, and its primary purpose is to initiate and control the execution of the entire program. In C++ programming, the main() function serves as the starting point for program execution and is, therefore, a compulsory component in every C++ program. This article covers the main() function in C++, its syntax, and its importance in programming.

Table of Contents

C++ main() Function

When a C++ program is launched, the initial function that gets executed is the main() function. It plays a crucial role in programming, as it is responsible for initiating and controlling the execution of the entire program. Understanding the use of the main() function is essential for writing C++ programs that function correctly.

Syntax of main() Function

The syntax for the main() function in C++ is as follows:

int main() {
   // program statements
   return 0;
}

The main() function always starts with the keyword int, indicating that the function returns an integer value. The function name is main, which is the standard name for the entry point of a C++ program.

The function name in C++ is usually followed by parentheses, which can contain parameters passed to the function. The statements that comprise the program are enclosed within the curly braces of the function body.

The value that a function should return to the operating system is specified by the return statement within the function.

Parameters of main() Function

There are two optional parameters that the main() function can take:

int main(int argc, char *argv[]) {
   // program statements
   return 0;
}

The first parameter is argc which is an integer that represents the number of arguments that have been passed to the program at runtime. The second parameter is argv which is an array of pointers to characters that stores the actual arguments that have been passed to the program.

Return Type of main() Function

The return value type of main() is always an integer. The integer value that main() returns to the operating system indicate whether the program executed successfully or encountered an error.

If the main function gives 0 it shows the code is executed successfully. Otherwise, if a non-zero value is output, it means the execution is not successful.

Execution of main() Function

When a C++ program is executed, the operating system calls the main() function to begin the program’s execution. The statements written inside the main() function are executed in a sequence such that the statement written first will be executed first and so on.

Once all the statements in the main() function have been executed, the function returns an integer value to the operating system, which then terminates the program.

Example Code

Here’s a simple example of a C++ program that uses the main() function:

#include <iostream>
int main()
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

In this example, the main() function simply prints the message “Hello, World!” to the console and then returns 0 which shows that the code is executed successfully.

Importance of main() Function

The main() function is the entry point of a C++ program, and its primary purpose is to initiate and control the execution of the entire program. It is responsible for receiving any command-line arguments passed to the program, initializing the program’s variables, and calling any necessary functions to start the program’s execution. Without the main() function, a C++ program would not be able to run.

The following restrictions apply to the main() function:

  • No other function within the program can be named main.
  • It is not possible to define the main() function as static or inline.
  • The main() function cannot be called from within the program.
  • The main() function address cannot be taken.
  • Overloading the main() function is not allowed in C++ programming.
  • It is not permissible to declare the main() function using the constexpr specifier.

Conclusion

The main() function is the entry point of a program and controls its execution. It is declared just beneath the header files and takes two arguments which are optional. The program will not execute without the main() function as it is an essential component of any C++ program.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.