Companion module: the C++ Foundations module is the complete, enriched version of this unit’s material — study these pages together for extra depth, practice sets, and the variation lab.</div>
Lesson 2 — Anatomy of a Program
Unit 01 · Session 1.2 · ~90–120 min · ← Lesson 1 · Unit index
In this lesson you will learn
- What every line of a minimal C++ program does — no magic left
- How
std::coutbuilds output piece by piece, and what\nandendlreally do - Why the compiler’s error messages are written the way they are — and a calm method for reading them
- How to write multi-line, nicely formatted output
You’ll need Lesson 1’s hello.cpp working before you
start.
1. Every line, explained
Here is Lesson 1’s program again — this time with nothing
left unexplained. The same file lives in the examples folder as
02_anatomy.cpp.
#include <iostream> ← 3
int main() { ← 4
std::cout << "Hello, world!\n"; ← 5
return 0; ← 6
} ← 7
Lines 1–2 — comments. // starts a comment: a note for humans that
the compiler completely ignores. It runs to the end of the line. Use
comments to say why the code does what it does — “what” is already
visible. (The /* like this */ form can span several lines; the course uses
// mostly.)
Line 3 — #include <iostream>. A directive for the compiler: “bring
in the declarations for the iostream library” — input/output
stream, the part of the C++ standard library that lets programs talk to
the console. Without this line, the compiler doesn’t know what std::cout
is. The angle brackets < > mean “a standard library header”.
Line 4 — int main() {. Every C++ program starts executing in the
function named main — this line defines it. The pieces:
| Piece | Meaning |
|---|---|
int |
this function hands back a whole number to the operating system when it finishes |
main |
the name the computer looks for when starting your program |
() |
the inputs to the function — main’s list is empty here |
{ |
opens the function’s body; everything until the matching } belongs to main |
Line 5 — the statement. std::cout << "Hello, world!\n"; — the
program’s one instruction (see §2 below). Statements end with ;.
Line 6 — return 0;. main hands the value 0 back to the operating
system. By long convention, 0 means success — “the program finished
normally”. (Other values can signal failure; more when we write our own
functions in Unit 07.)
Line 7 — }. Closes main’s body. Every { must have a matching
} — mismatched braces produce some of the most confusing beginner error
messages, so pair them from the start. Editors like VS Code highlight the
matching brace when your cursor is on one.
What is
std::?stdis the namespace of the C++ standard library — a family surname for thousands of standard names likecout. Writingstd::coutsays “thecoutthat belongs tostd”. You’ll see why this precision is a feature when programs get bigger; for now, type it as is.
2. std::cout — output, piece by piece
std::cout (“see-out”, character output) is the console output
stream: a one-way pipe from your program to the screen. You push things
into the pipe with <<:
std::cout << "Hello, world!\n";
Read << as “sends”. std::cout sends the text to the console.
<< chains — you can send several things in one statement:
std::cout << "The answer is " << 42 << "\n";
Output:
The answer is 42
Notice three things: text goes in double quotes "..."; the number 42
went in without quotes (the stream knows how to print plain values); and
<< just kept passing things along. The runnable version is
03_output_basics.cpp — open it, compile
it, and modify it as you read.
\n vs endl — two ways to end a line
| You write | Effect | Which to use |
|---|---|---|
\n |
a newline character — start a new line | ✅ the course default |
std::endl |
a newline and an immediate flush of the output buffer | when timing matters (rare at our level) |
\n lives inside the string: "Hello\n". endl is a name from the
library, written outside the string: std::cout << "Hello" << std::endl;.
Both produce the same visible output here. Why two? The full story — output
buffering — is a Unit 12 topic. Until then: use \n, and know that
endl is not wrong, just usually unnecessary.
Special characters you can print
| In a string | Prints |
|---|---|
\n |
newline |
\t |
tab |
\" |
a double quote |
\\ |
a single backslash |
The backslash is the “escape” character: it changes the meaning of the next
character. That’s also why printing a real backslash needs two: "\\".
3. Multi-line output, nicely
Combining chaining, \n, and \t gives you formatted output already:
#include <iostream>
int main() {
std::cout << "==== STUDENT CARD ====\n";
std::cout << "Name : Ayesha Khan\n";
std::cout << "Year : First\n";
std::cout << "=======================\n";
return 0;
}
Output:
==== STUDENT CARD ====
Name : Ayesha Khan
Year : First
=======================
One statement per line of output is the clearest habit while you’re new.
Later (Unit 03) you’ll learn formatting — aligning columns, controlling
decimals — but plain cout already covers a lot.
4. Reading compiler errors without panic
A compiler error is not scolding — it is the compiler saying precisely where it got confused. Learn to read them and they become one of your best tools.
hello.cpp: In function 'int main()':
hello.cpp:5:22: error: expected ';' before '}' token
5 | std::cout << "Hello, world!\n"
| ^ ~
| ;
How to read it, piece by piece:
| Part | Meaning |
|---|---|
hello.cpp:5:22 |
file, line 5, column 22 — where the compiler got stuck |
error: |
it cannot proceed (a warning: would mean “suspicious, but proceeding”) |
expected ';' |
what it needed |
before '}' token |
where it needed it — the compiler’s gaze is at the }, so the missing ; is just before it |
the source excerpt with ^ |
the compiler pointing at the exact spot |
The two rules that make errors harmless:
- Fix the first error only, then recompile. Later errors are usually
echoes of the first — a single missing
;can spawn five errors. - The reported line is often after the real mistake. “Expected
;before}” means the mistake is one line earlier. Read upward.
The most common first-week errors and their fixes are collected in the Compiler Error Catalogue — open it whenever an error confuses you, and add your own finds to your bug diary.
5. Predict, then check
Write your answers before compiling anything:
- What does this print?
std::cout << "A\nB\nC\n"; - What does this print — exactly, spacing included?
std::cout << "X\tY\n" << "1\t2\n"; - What does this print?
std::cout << "He said \"hi\"\n"; - Which of these two lines is the course default, and why?
std::cout << "One\n"; std::cout << "Two" << std::endl;
Reveal answers (write yours down first!)
A1. Three lines:
A
B
C
Each \n ends the current output line and starts a new one.
A2. Two lines, tab-aligned in columns:
X Y
1 2
\t jumps to the next tab stop; the chained << sends both strings in one
statement, but the \n inside the first string still ends the line.
A3.
He said "hi"
\" prints a literal double quote — the string can contain quotes when
they are escaped.
A4. The \n line. Both print identically here, but std::endl also
forces a flush of the output buffer — extra work we don’t usually need.
Reserve endl for the rare cases where flushing timing matters (a Unit 12
topic); the course default is \n.
Check yourself
- I can name what a comment,
#include,main, a statement, andreturn 0;each do - I can explain
std::coutand<<in one sentence - I know when to use
\nvsendl(and can say whatendladditionally does) - I can print a string containing
"and\correctly - Given a compiler error, I read line number → message → fix the first error only
Next: Lab 01 — First Program Lab (build + Debug It), then the exercises and quiz.