Your First C++ Program
What every line of a C++ program actually does, and why the compiler is a separate step you have to think about.
8 min readUpdated 2026-09-01cppbasicsgetting-started
The smallest complete program
cpp
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Four lines, and every one of them is doing something worth understanding. Most tutorials tell you to copy this and move on. That is exactly why people get stuck two weeks later.
#include <iostream>
The # marks a preprocessor directive. Before the compiler sees your code, the preprocessor runs and literally pastes the entire contents of the iostream header into your file.
That matters: C++ has no idea what std::cout is until you include the header that declares it. Forget the include and you get an error about cout not being declared — not "you forgot an import", which is what a Python programmer expects.
int main()
Execution starts here. Every C++ program has exactly one main, and the operating system calls it for you.
The int is the return type. main hands an integer back to the OS when it finishes: 0 means success, anything else means failure. This is why return 0; is on the last line.
std::cout << "Hello, world!"
std::cout is the standard output stream. The << operator sends data into it.
The std:: prefix is a namespace. Everything in the standard library lives inside std so that its names cannot collide with yours. If you write your own function called count, it will not clash with std::count.
std::endl
Prints a newline and flushes the output buffer. That flush is not free.
Most of the time you want'\n' instead:
cpp
std::cout << "Hello, world!" << '\n'; // faster, no forced flush
endl can be noticeably slower. Use it only when you genuinely need output to appear immediately.
Compiled, not interpreted
This is the single biggest shift if you are coming from Python or JavaScript.
Python runs your source directly. C++ does not. Your.cpp file must be compiled into machine code before it can run:
text
hello.cpp ──compile──▶ hello.exe ──run──▶ output
bash
g++ hello.cpp -o hello # compile
./hello # run
Three consequences follow, and they explain most beginner confusion:
- Errors are caught before the program runs. A typo on line 200 stops compilation even if line 1 was fine. Python would have happily run until it reached line 200.
- Types are checked at compile time. Assigning a string to an
intfails to compile. It is not a runtime error. - You must recompile after every edit. Forgetting this is why "my change did nothing" happens — you ran the old binary.
About using namespace std;
You will see this everywhere:
cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << '\n'; // no std:: needed
return 0;
}
std into your scope so you can drop the prefix.
It is fine for competitive programming and small exercises. It is a genuinely bad habit in larger code, because std contains hundreds of common words — count, distance, max, data, size, swap. Any of them can silently collide with your own names, and the resulting compiler errors are famously hard to read.
A good middle ground:
cpp
using std::cout;
using std::cin;
std:: explicitly so you always know where a name comes from.
Reading input
cpp
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "You are " << age << " years old.\n";
return 0;
}
std::cin is the input stream, and >> pulls data out of it. The direction of the arrows is a useful mnemonic: data flows into cout, and out of cin.
Try it
Predict the output before running it.
What does this print?cpp
Loading editor…
3 then 1. Integer division truncates toward zero — it does not round. 7 / 2 is 3, not 3.5 and not 4. This catches almost everyone once.
Common mistakes
- Missing semicolon. Every statement ends with
;. The reported error often points at the next line, because that is where the compiler notices something is wrong. - Forgetting the include.
coutwas not declared means you are missing<iostream>. - Confusing
=and==.=assigns,==compares.if (x = 5)assigns 5 toxand is always true. - Expecting
intdivision to produce a decimal. Use7.0 / 2or cast one operand. - Not recompiling. Editing the source does not change the binary.
- Using
endleverywhere. Prefer'\n'unless you need the flush.
Summary
#include pastes in declarations, main is the entry point, std:: is the standard library namespace, and <</>> move data through streams. C++ compiles to machine code before running, so type errors and typos surface at compile time rather than mid-execution.