C++ · Lesson 1 · On the house

A program that says something

Include, main, and std::cout. Lesson 1 is free — on the house.

Lessons · C++ · Lesson 1 of 10 · On the house

A program that says something

Include, main, and std::cout.

Illustration for the C++ course
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

You're writing instructions for a machine that has no common sense and infinite patience — but unlike a scripting language you may have tried, C++ asks you to compile first. You type plain text into a .cpp file. A compiler (g++ or clang++) turns that text into a binary. You run the binary. Feedback comes back as output or as a diagnostic. That write–build–run loop is how you'll learn every later idea — not by memorizing posters. When the compiler complains, read the first error first; later errors are often fallout from the first mistake. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Saving a file is not building it. Compile, read the first diagnostic, fix, repeat. The build line is part of the language experience — treat it as seriously as the source. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

A C++ program needs an entry point named main. The operating system calls main when your program starts. For today, int main() { ... return 0; } is the whole shape: a return type, a name, empty parameter list, a body in braces, and a success code. return 0; tells the OS the program finished normally. Other return values can signal failure later; today, zero means done and fine. Braces mark scope in C++. A missing brace or an if without braces that later gains a second statement is a classic footgun — prefer braces even for one-liners while you're learning. Concurrency multiplies every bug. Start with one clear ownership story and a join (or equivalent) you can point at. Clever lock-free ideas come after boring correctness. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

#include is a preprocessor directive. It pulls in declarations for the standard input/output streams so you can talk to the terminal. Angle brackets mean “look in the standard include paths.” Without this include, the name std::cout is unknown and the compile fails. Includes are not decoration — they're how C++ finds the pieces of the library you're borrowing. Watch quotes and angle brackets carefully. Headers use <...>; string literals use "...". Smart quotes from a chat app will break a compile in a way that looks cursed until you retype them straight. Saving a file is not building it. Compile, read the first diagnostic, fix, repeat. The build line is part of the language experience — treat it as seriously as the source. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

std::cout is the character output stream in namespace std. The << operator inserts values into that stream. You can chain insertions: std::cout << "hello" << std::endl; First the string literal, then std::endl which ends the line and flushes the buffer. Flushing matters when you debug; for a hello world, endl is a clear, honest newline. Some people prefer '\n' for speed; today prefer clarity. Watch quotes and angle brackets carefully. Headers use <...>; string literals use "...". Smart quotes from a chat app will break a compile in a way that looks cursed until you retype them straight. Zero-based indexing is the tax you pay once and forever. When something looks one-off, count from zero on paper before you blame the standard library. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

String literals use double quotes: "hello". Those quotes are for the compiler, not for the screen — the program prints hello without quote marks. Semicolons end statements in C++. Forgetting one is a classic first-day error. Curly braces open and close main's body. Match them. Smart quotes copied from a chat app will break the compile; retype straight quotes. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Braces mark scope in C++. A missing brace or an if without braces that later gains a second statement is a classic footgun — prefer braces even for one-liners while you're learning. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

You need a compiler on your PATH. On many Unix-like systems, g++ main.cpp -o main then ./main is the ritual. Saving the file is not compiling it. If nothing prints, confirm you actually built and ran the binary. Your first victory should be boring: one program, clear output, no mystery left unexplained. Saving a file is not building it. Compile, read the first diagnostic, fix, repeat. The build line is part of the language experience — treat it as seriously as the source. A type is a bundle of data plus the operations that keep it honest. Constructors establish invariants; destructors release resources. That pairing is RAII — the heartbeat of safe C++. Before you build, predict what A program that says something should do on the still's inputs. Prediction turns compiling into confirmation.

hello

#include <iostream>

int main() {
    std::cout << "hello" << std::endl;
    return 0;
}
The smallest talking C++ program.

Walk the still top to bottom. #include brings stream declarations. int main() is the entry point. Inside the braces, std::cout << "hello" << std::endl; inserts the string and a newline-flush into the output stream. return 0; signals success. No class, no vector, no template — just the skeleton every later program still stands on.

Pitfalls: capital Main (wrong); missing semicolon; smart quotes; forgetting #include; writing print("hello") like Python; omitting return type; putting code outside main with nowhere to run it from. Literal language, calm retries. Keep a scratch file and rebuild after every change.

Quiz

What header provides std::cout?

Quiz

What is the program entry point called?

Quiz

What does << do with std::cout?

Quiz

What does return 0; usually mean in main?

Quiz

Why do you need #include <iostream> before using std::cout?

Quiz

What does std::endl do in the still?

Check

Type a program that prints hello with a newline (match the still).

That's lesson one: text in, compile, text out. Next we give values typed names that stick around. Mark it passed when checks are green — you actually programmed.

Open-book: the answers are on this page. Pass every quiz and check (7) to mark the lesson done. This visit: 0/7.

A program that says something · C++ (free) — The Gold Standard