How C++ Programs Work
Understand source files, compilation, linking, and execution, then run and modify a complete C++ console program without assumed experience.
Explain the separate jobs of the compiler and linker
Read the structure of a complete console program
Run, change, and verify a small C++ program
The short answer
You write C++ source code, a compiler translates each source file, a linker combines the results with required libraries, and the operating system runs the executable. Start with a complete program and change one observable behavior at a time.
From source text to an executable
A C++ source file is text that follows the language grammar. The compiler parses that text, checks types and syntax, and produces object code. A linker then connects object files and required library code into an executable. This two-stage model explains why a missing semicolon is a compile error while a declared-but-undefined function often becomes a linker error.
Execution is another stage. The operating system loads the executable and begins at main. A program can therefore compile correctly and still fail while running, or run to completion and calculate the wrong answer. Naming the stage of failure is the first debugging skill.
| Stage | Job | Typical failure |
|---|---|---|
| Compile | Check and translate one source unit | Unknown name or incompatible type |
| Link | Connect compiled definitions | Function declared but never defined |
| Run | Execute the finished program | Invalid input or out-of-range access |
| Verify | Compare behavior with the requirement | Correct execution, wrong answer |
Read the smallest useful program
Every hosted C++ program has a main function. Braces contain its body, statements normally end with semicolons, and return 0 reports successful completion. The std::cout object writes characters to standard output. The insertion operator sends each value on its right into that stream.
#include <iostream>
int main()
{
std::cout << "First line\n";
std::cout << "Answer: " << 6 * 7 << '\n';
return 0;
}Expected output
First line Answer: 42
Headers namespaces and the standard library
The #include directive makes declarations from a header available before compilation. The iostream header declares standard stream facilities. std:: qualifies a name from the standard-library namespace and prevents collisions with names from your program or another library.
Avoid a global using namespace std; in course examples. It saves a few characters but imports a large set of names into the current scope. Writing std::cout, std::string, and std::vector keeps ownership visible and scales better as a program gains dependencies.
Build with a prediction loop
Run the original program once, make one small change, predict its effect, and run again. This edit-build-observe loop connects source to behavior. If several changes are made together, a surprising result has several possible causes.
Diagnostics are evidence, not a score. Begin with the earliest useful error because later messages may be consequences. Copy the exact message, inspect the named location and nearby punctuation, then test one correction.
Quick knowledge check
Answer before you reveal.
01Does the compiler normally run your program?
No. It translates source code and reports compile-time problems. The operating system runs the linked executable afterward.
02What does a zero return value from main conventionally mean?
It tells the environment that the program completed successfully.
Exercise
Practice challenge
Change the starter program to print your name, the kind of software you want to build, and the result of 7 + 5 on separate lines. Predict the exact output before running it.
Requirements
- The program compiles and exits successfully
- Each requested value appears on its own line
- You can identify which statement produced each output line
Optional extension: Remove one semicolon, read the first diagnostic, and restore the program.
Open in C++ compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Is C++ the same language as C?
No. C++ shares historical syntax with C but has its own type system, standard library, object model, templates, and language rules.
Do I need an IDE for this course?
No. The browser compiler covers single-file exercises. Use a local compiler and editor when the course reaches multiple files and tests.
Which C++ version does this course teach?
It teaches broadly supported modern C++ practices and calls out features that require a newer compiler rather than depending on one vendor.