How a C++ program starts and executes
After a toolchain produces a runnable program, the operating system starts it. In an ordinary hosted C++ program, startup leads to the special function named main; statements inside then perform the program’s work.
Learning objectives
- Describe the difference between building a program and running it.
- Explain the role of
mainin a hosted C++ program. - Trace simple statements in their execution order.
- Recognize which startup details belong to the operating system and implementation rather than to portable C++ source.
Prerequisites: no prior C++ knowledge is required. Familiarity with files, folders, and running a program from an IDE or terminal is helpful.
Build time versus run time
Build time is when tools translate and link your source code. Run time is when the resulting program is actually executing. A typo in a name may prevent a build. A program that divides by an unexpected value or receives invalid input can have a problem at run time. Keeping these moments separate helps you read errors accurately.
From launch to main
On a typical desktop operating system, launching an executable causes the operating system to create a process: a running instance with resources such as memory and handles to input/output destinations. A loader prepares the executable and required shared components according to the platform. Startup code then leads into the C++ program. The portable C++ rule for an ordinary hosted program is that execution begins with the function named main.
you launch an executable
↓
operating system creates a process (typical hosted environment)
↓
platform loader and startup work prepare the program
↓
C++ program reaches main()
↓
statements in main perform work
↓
main returns and the program terminatesA tiny execution trace
#include <iostream>
int main() {
std::cout << "First\n";
std::cout << "Second\n";
return 0;
}Code, line by line
#include <iostream>makes declarations for standard stream output available to the program.int main()defines the main function. Itsintreturn type communicates an integer status to the execution environment.- The opening brace begins the function body: the group of statements that make up this function.
- The first
std::coutstatement writesFirstfollowed by a newline to standard output. - The second statement writes
Secondfollowed by a newline. return 0;endsmainand supplies status 0. Reaching the end ofmainhas the effect of returning 0 in a hosted C++ program, but writing it explicitly is helpful while learning control flow.- The final brace ends the function definition.
How this example proceeds internally
- Startup reaches
main. - The first output expression is evaluated. Its characters are sent to the program’s standard output stream.
- After that statement completes, evaluation continues with the second statement, which writes its characters.
- The return statement finishes
mainwith status 0. The environment performs termination work appropriate to the program and platform.
This is a source-level trace. A processor ultimately executes target-specific instructions generated by the toolchain. The C++ standard describes required observable behavior through an abstract machine; it does not require a real CPU to imitate each source line as one physical instruction.
Why main exists
A larger program can contain many functions. The environment needs an agreed place to begin the program’s work. In a hosted C++ program, main is that designated entry function. It can call other functions, which may call more functions, and then control returns to the caller when each finishes.
Common beginner mistakes
- Thinking that the computer starts at the first line of the file: directives, declarations, and functions have different roles. For a simple hosted program, execution begins at
main, not merely at the first visible character. - Calling
mainjust another reusable function: it is special in C++; do not call it recursively or take its address. - Assuming return 0 prints something: it reports a completion status; it is separate from
std::coutoutput. - Assuming one source statement equals one processor instruction: the compiler and target architecture determine low-level instructions while preserving required language behavior.
- Forgetting that output destinations vary: “standard output” may be a terminal, redirected file, test harness, or another environment-provided destination.
Practical exercises
- Write a paper trace for the example: list the contents of standard output after each statement.
- Swap the two output statements. Predict the result before compiling, then verify it.
- Remove
return 0;and run the program. Explain why the output remains the same for this hosted example. - Ask your compiler or IDE where the program’s exit status is shown. Run a program that returns 0, then one that returns 1, and record what your environment reports.
Show worked exercise solutions
- After the first output statement, standard output contains
Firstand a newline. After the second, it containsFirst, a newline, thenSecond, and a newline. The return statement produces no printed text. - The output becomes
Secondon the first line andFirston the second line, because the statements execute in their new source order. - In a hosted C++ program, reaching the closing brace of
mainhas the same effect asreturn 0;. The program's output is unchanged. - The process exit status is 0 for the first program and 1 for the second. How your terminal or IDE displays it depends on the environment; for example, a shell may expose the most recent exit status through a platform-specific variable.
Assessment: check your understanding
1. What is the difference between build time and run time?
Answer: Build time translates and links source code; run time is when the resulting program is executing. Why: A source syntax diagnostic belongs to the build, while behavior produced after launch belongs to execution.
2. In a hosted C++ program, what function is the designated entry point?
Answer: The global function named main. Why: The C++ rules designate it as the start of a hosted program.
3. What does return 0; do in this example?
Answer: It finishes main and returns status 0 to the environment. Why: It does not print a zero; printing is done by the output statements.
4. Must every C++ program run under a desktop operating system?
Answer: No. Why: C++ has freestanding implementations where the startup arrangement can be implementation-defined.
5. Does the C++ standard require one std::cout statement to become one physical CPU instruction?
Answer: No. Why: The standard specifies observable behavior through an abstract machine, while implementations choose the lower-level strategy.
Revision summary
A toolchain creates a runnable program; the operating system starts it in a typical hosted environment. C++ startup reaches main, where statements are evaluated in sequence unless control flow changes that order. The next lesson writes the smallest complete program with enough detail to understand every line.