Main function in C++

This is a guide on the main function in C++.

In the context of computer programming, particularly within languages such as C and C++, a function can be understood as a self-contained, named block of code that has been designed to carry out a specific, well-defined task. The key advantage of using functions is that they are reusable, meaning that once you have written a function, you can invoke it multiple times from different parts of a program without having to rewrite its code each time. Among all the functions that can exist in a C++ program, one stands out as especially significant: the main() function. This particular function is considered special because it serves as the designated entry point for the program. In other words, when you run a C++ program, the main() function is the first piece of your code that gets executed automatically by the system. Every standard C++ program must contain exactly one main() function; without it, the program will not know where to begin execution, and the compilation process will fail.

If you are using an Integrated Development Environment, commonly referred to as an IDE, to create a new C++ project, you will typically find that the IDE automatically generates a skeleton or template for the main() function. This is a convenience feature intended to save you from having to write the basic structure from scratch each time. The generated code may vary slightly depending on which IDE you are using, but it almost always includes the essential elements of the main() function.

The declaration of the main() function begins with the C++ keyword int. This int keyword is used to specify the type of data that the function will return once it finishes its execution. In this case, int indicates that the main() function returns an integer value—a whole number—back to the operating system. You will see the significance of this integer return value explained in more detail shortly. Immediately after specifying the return type, you write the name of the function, which is main, and this is followed by a pair of parentheses. The parentheses are part of the function’s syntax and are used to enclose any parameters that the function might accept; for the main() function, these parentheses are mandatory even if no parameters are passed.

When declaring the main() function, you have two valid options regarding the content of the parentheses. You may either leave the parentheses completely empty, which is perfectly acceptable, or you may include two specific parameters inside them. These two parameters are conventionally named argc (argument count) and argv (argument vector), and they allow the program to receive command-line arguments from the user. However, you do not need to worry about these parameters for now, as they are entirely optional. In fact, for the purposes of the programs presented in this material, these parameters are not used at all. Even though some IDEs automatically include argc and argv by default when generating the main() function, you can safely ignore them or remove them if you wish, because the example code does not rely on them.

Following the function header—that is, the line containing int main()—the next component of the main() function is the opening brace, denoted by the symbol {. This opening brace marks the beginning of the function’s body, which is the block of code that contains all the statements and instructions that the function will execute. According to many widely adopted C++ style guides, it is considered a best practice to place this opening brace on its own separate line, rather than on the same line as the function header. This stylistic choice improves code readability by clearly visually separating the function’s signature from its internal implementation. Consistent with that recommendation, this guide will also place the opening brace on its own line throughout all examples.

The body of the main() function is where you write the actual statements that constitute your program’s logic and behavior. These statements are executed sequentially from top to bottom when the program runs. The very last statement inside the body of the main() function is typically a return statement. The purpose of this return statement is to send an integer value back to the operating system when the program finishes. This returned integer acts as a status code: it tells the operating system whether the program terminated normally (without errors) or abnormally (due to some problem). By convention, a return value of 0 signifies successful, normal termination, whereas any non-zero value typically indicates an error or abnormal termination. However, it is important to note that if you do not explicitly include a return statement at the end of the main() function, the C++ language standard guarantees that the compiler will automatically insert a return 0; statement on your behalf. Because of this default behavior, many C++ programmers choose to omit the return statement entirely from their main() functions, relying on the implicit return of 0.

If you are working with a version of C++ that is C++11 or later, you have an additional option for writing the return statement. Instead of returning the literal integer 0 or some other number, you can return the predefined macros EXIT_SUCCESS and EXIT_FAILURE. These macros are defined in the <cstdlib> header (or <stdlib.h> in C) and are more descriptive than raw integers. Specifically, EXIT_SUCCESS is defined as 0, and EXIT_FAILURE is defined as a non-zero value, typically 1. The main advantage of using EXIT_SUCCESS and EXIT_FAILURE is that they make the intent of the return statement much clearer and more readable to anyone reading the code. Nevertheless, because the main() function automatically returns a value indicating a normal exit (i.e., 0 or EXIT_SUCCESS) even when no explicit return is written, you will find that in practice, you rarely need to code an explicit return statement inside your main() function at all.

Finally, the last line of code in the main() function is the closing brace, represented by the symbol }. Unlike the opening brace, which is often placed on its own line but could technically be placed elsewhere, the closing brace should always appear on its own separate line. This practice is strongly recommended because one of the most common syntax errors in C++ programming is forgetting to include a closing brace to match an opening brace. By leaving the closing brace on its own line, you can more easily scan your code and verify that every opening brace has a corresponding closing brace, which helps prevent compilation errors and makes the structure of your code visually clear. To summarize, a typical, minimal main() function in C++ appears as follows:

text
int main()
{
    statements;
    return 0;
}