Your First C++ Program

Since the 1970s, the first program written in a new language has traditionally been one that prints "Hello World!" to the screen. The tradition survives because it is genuinely useful: it is the smallest complete program that proves your tools work end to end — you wrote source code, the compiler accepted it, and the computer executed it.

It is also worth studying closely. Every piece of this five-line program appears in every C++ program you will ever write. Understand it once and you understand the skeleton of all of them.

#include <iostream>

int main() {
    std::cout << "Hello World!" << '\n';
    return 0;
}

When compiled and run, it prints:

Hello World!

Let's take it apart, line by line.

#include <iostream>

This is a preprocessor directive — an instruction that runs before compilation begins. It tells the compiler to pull in the contents of iostream, the part of the C++ standard library that handles input and output (the name is short for input/output stream).

Our program needs it for one reason: std::cout is defined there. Remove this line and the compiler no longer knows what std::cout is, and compilation fails.

int main()

This defines a function — a named block of code — called main. It is not just any function: main is the entry point of every C++ program. When you run your program, the operating system calls main, and execution proceeds from its first statement to its last.

Every C++ program must have exactly one main function. The int at the front says that main hands back an integer value when it finishes — we will see that value at the bottom of the program.

The braces { and } mark where the function's body begins and ends. Everything between them is what main actually does.

std::cout << "Hello World!" << '\n';

This is a statement — a single instruction, and in C++ most statements end with a semicolon. This one has four working parts:

  • std::cout (pronounced "see-out", short for character output) is the standard library's connection to your screen. The std:: prefix says that cout lives in the standard library's namespace — for now, read std::cout as one name; namespaces get a proper explanation later in the course.
  • << is the insertion operator. It sends whatever is on its right into the output stream on its left. You can chain it: this statement first sends the text, then sends a newline.
  • "Hello World!" is a string literal — text data, always wrapped in double quotes. The quotes are how the compiler tells your message apart from actual code.
  • '\n' is the newline character, which moves the output cursor to the next line — the same effect as pressing Enter. Single characters use single quotes; text of any length uses double quotes.

return 0;

When main finishes, it returns an integer to the operating system called a status code. By convention, 0 means "the program ran successfully"; any non-zero value signals that something went wrong. This is the integer that int main() promised to deliver.

Looking ahead
The standard library also provides the named constant EXIT_SUCCESS as a more readable alternative to 0. A later lesson covers status codes in detail.

The Syntax That Trips Up Beginners

Three punctuation rules do most of the damage in early programs:

  • Semicolons ; terminate statements. Forget one and the compiler complains — usually about the next line, which is the first place it notices something is wrong.
  • Braces { } group code together. Every { needs a matching }.
  • Double quotes " " wrap text you want treated as data rather than code.

For example, dropping the semicolon after '\n' produces a compiler error like expected ';' before 'return'. Compiler errors look intimidating at first, but they almost always name the exact problem — reading them is a skill we build deliberately in the upcoming lessons on C++ errors.

Make It Yours

The fastest way to make this program feel less like a ritual and more like a tool is to change it. Print your own message:

std::cout << "Hello, I'm Dana, and this compiles." << '\n';

Or print several lines — statements run in order, top to bottom:

std::cout << "Line one" << '\n';
std::cout << "Line two" << '\n';

Summary

  • #include <iostream> is a preprocessor directive that gives the program access to the standard library's input/output tools, including std::cout.
  • main is the entry point: execution starts at its first statement. Every program has exactly one.
  • std::cout writes to the screen; the insertion operator << sends data into it; '\n' starts a new line.
  • return 0; reports success to the operating system.
  • Statements end with semicolons, braces delimit blocks, and double quotes mark text data.

Next, we look at statements more closely — the individual instructions that every program is built from.