Lesson 1
Hello World in C++, line by line
Every C++ programmer's first program — and every line in it has a job. Run it right here, then we'll take it apart.
The program
Press Run — the code executes inside your browser, nothing to install:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
return 0;
}
What each line does
| Line | What it means |
|---|---|
#include <iostream> | Brings in the input/output stream library, which gives you cout (printing) and cin (reading input). Without it, the compiler has no idea what cout is. |
using namespace std; | The standard library puts its names inside a "namespace" called std. This line lets you write cout instead of std::cout. Handy in lessons; many pros prefer writing std:: explicitly in real projects. |
int main() { ... } | The entry point. Every C++ program starts running here. The int means it hands a whole number back to the operating system when it finishes. |
cout << "..." << endl; | Prints text. << is the "insert into output" operator — you can chain several in one line. endl ends the line (like pressing Enter). |
return 0; | Tells the OS "all good". Zero means success; non-zero usually signals an error. |
Now make it interactive: reading input
Type a number in the input box, then Run. cin reads it, and the program does math with it:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
cout << "In ten years you will be " << age + 10 << "." << endl;
return 0;
}
Two things to notice: the prompt ("Enter your age: ") is printed with cout using <<, and the value comes in with cin using the opposite arrow, >>. Easy way to remember: data flows in the direction the arrow points.
Classic beginner mistakes (we've all made them)
- Missing semicolon. C++ statements end with
;. The compiler will complain with a confusing message on the next line — when you see an odd error, check the line above it. - Wrong case.
coutworks,CoutandCOUTdo not. C++ is case-sensitive everywhere. - Forgetting
using namespace std;— thencoutis "undeclared". Fix either by adding that line or writingstd::cout. - Unmatched quotes or braces. Every
"needs a partner, every{a}.
Running it on your own computer (for later)
The playground above is perfect for lessons and quick tests, but sooner or later you'll want a real compiler on your machine. The two friendliest free options:
- Windows: install Visual Studio Community (choose the "Desktop development with C++" workload) — it's an editor + compiler + debugger in one.
- Mac: install Xcode from the App Store, or just the command-line tools (
xcode-select --installin Terminal) and compile withclang++ main.cpp -o main. - Linux:
sudo apt install g++and compile withg++ main.cpp -o main.
Practice: try it yourself in the playground
- Change the Hello World program to print your name and today's date on two separate lines.
- Modify the age program to also print your age in months (
age * 12). - Break the program on purpose: remove a semicolon and read the compiler error. Learning to read errors now saves hours later.
Next up: variables and types — how C++ decides what 3, 3.0 and "3" really mean.