When the operating system loads a C++ program, it searches for a single function that it can call. The name of this function has to be main. The program is run from within main, and the program terminates once main terminates.
So a minimal program, then, is just this function with no other.
int main()
{
return 0;
}
Starting with the line int main() defines a function called main that returns an integer value. The braces contain the body of the function, the code that is executed when the function is called. The return 0; statement returns the integer 0 to the operating system.
By long standing convention, a program that exits with this return code has not encountered an error.
If this function is omitted, or if it is called anything other than main, then the linker will report that there is no entry point and it will not produce an executable file.
The body of main can have as many non protected statements as needed. The following program writes a short message and then returns success.
#include <iostream>
int main()
{
std::cout << "starting\n";
std::cout << "finished\n";
return 0;
}
Execution starts at the first statement inside main. The two insertion events send text to the console, and then return 0; ends the function. The OS receives the value 0 and considers the run as successful. The two events are executed in the sequence that they appear; there are no naturally looping or ignoring.
The only piece of information that main is mandated to return to the operating system about how the program terminated is the integer value returned. The accepted convention is that returning a value other than zero indicates an abnormal termination:
#include <iostream>
int main()
{
std::cout << "an error occurred\n";
return 1;
}
The message is printed and the value 1 is returned to the operating system. Scripts or other programs that started the program can inspect that value and do whatever they want; here, nonzero integers are enough to indicate failure. Specific nonzero values are the decision of the programmer.
In modern C++, it is also valid to leave out a return from main. When control reaches the closing brace, the compiler inserts a return 0; on your behalf.
#include <iostream>
int main()
{
std::cout << "done\n";
}
The program prints done and then returns 0 even though there is no return in the source. Explicitly writing return 0; is still useful as a learning tool as it shows the success code to the human reader rather than hiding it inside an implicit return.
The name main is only special because the operating system searches for it. Within the language itself, main is just an ordinary function: it has a return type, it can declare local variables, and the statements are executed sequentially. The only restrictions here are that the program must have exactly one definition of main and that its return type must be int.
If the return type is not int, then the program is not standard.
void main() // incorrect
{
}
Most compilers will give a warning, and the resultant program isn't portable. When the signature is exactly int main(), it conforms to the rules of the language and will return the integer that the programmer intended to the operating system.
Since running the whole program is the same as running main, any statement that appears outside of a function is illegal. The compiler considers this text to be an error before it even makes an executable. So, everything the program does, calculations, user interaction, decisions, must be contained in main, or in functions called by main.
Because of that, the function main is all that every C++ program ever is in the beginning and in the end. It is being called by the operating system, and every statement in it is being executed one after another, and the number it's returning to the operating system is saying if the run was considered successful or not. As soon as this simple contract is understood, everything else in the language can be studied knowing precisely where the execution is beginning and ending.
Exercise: Understanding main()Starter code, hints and solutionOpenClose
Practice: Understanding main()
Work through the mission, reveal hints only when needed, and compare your result with the model solution.
Your challenge
Change finished to All done while keeping starting and return 0. Predict the output order and distinguish the printed text from the exit status.
Mission checklist
- Run the starter as a baseline, then predict the change requested in Your challenge.
- Make that change in your editor; use the lesson-specific hints if you need help.
- Compile and run your answer, then compare with the worked solution and explain the result.
Starter program
This is the working baseline, not the completed answer. Compile as C++20, then make the change described above.
#include <iostream>
int main()
{
std::cout << "starting\n";
std::cout << "finished\n";
return 0;
}Hints
main begins with the statement that prints "starting". Its return statement ends the function; statements placed after that return will not run.
Change one of the two messages, leaving return 0 in place. The printed text and the integer exit status serve different purposes.
Worked solution
The second message is now All done. main prints starting first, then All done, and returns the exit status 0; the return value is not printed text.
#include <iostream>
int main()
{
std::cout << "starting\n";
std::cout << "All done\n";
return 0;
}
Example output
starting
All done
0 comments
No comments yet. Be the first.