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

LineWhat 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)

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:

Practice: try it yourself in the playground

  1. Change the Hello World program to print your name and today's date on two separate lines.
  2. Modify the age program to also print your age in months (age * 12).
  3. 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.