C++ from the ground up

Your first C++ program

What C++ is, what the compiler does before your program runs, and the smallest complete program: one function, a few lines of output, and a run.

Part 1 of 12

Beginner13 min readBack to Courses

What C++ is for

C++ is a language for programs that have to be fast and have to run close to the machine: game engines, browsers, database servers, trading systems, the software inside a car. It grew out of C, so it keeps C speed, and it adds the tools that make large programs possible — classes, templates, and a library of containers and algorithms.

The trade is that you say more. C++ asks you to name the type of every value, to include the parts of the standard library you use, and to end each statement with a semicolon. In exchange, the compiler can check a great deal before anything runs, and the finished program is small and quick.

The shape of every program

A C++ program starts at a function called main. That is the function the operating system calls, and everything your program does happens because main asked for it. This page runs a single file, so every example here is one main.cpp with a main in it.

Before a program can run it has to be compiled: the compiler reads your text and writes a machine-code file, and if it finds something it does not understand it stops and tells you the line. That is why a run here takes a moment — a real compiler is doing real work.

Printing, and where the words come from

Output goes through std::cout, which is the standard output stream. The name has two parts: std is the namespace — the standard library keeps everything it owns in there so your names cannot collide with its names — and cout is the stream itself. You write std::cout << and hand it values one after another.

<< is the insertion operator, and it means "send this to the stream". \n is a newline character written inside a string. The standard says "std" out loud on every use, and this course keeps it that way: a bare using namespace std; at the top of a file makes every standard name visible at once, which is exactly how two libraries with a count or a sort end up fighting over your file.

\n and std::endl both end a line, and they are not the same thing. \n writes a newline character; std::endl writes one and then flushes the stream, forcing the output out immediately. Flushing matters when a program is being watched live or might stop before the buffer empties; for ordinary printing, \n is enough and cheaper, which is why it appears throughout this course.

One program, end to end

#include <iostream>
#include <string>

int main() {
    std::string language = "C++";
    int year = 1985;

    std::cout << "Hello from " << language << "!\n";
    std::cout << "The first standard was " << year << ".\n";
    return 0;
}

Run it, then make it yours

Press Run to see the program compile and print. Then change the language name and the year, add one more cout line of your own, and run again.

Output
Click "Run" to execute your code

Change a value and press Run again. The run goes to a real compiler on the server, so the first one may take a few seconds — after that the round trip is quick.

What the compiler does before the program runs

The compiler is a pipeline rather than a single program. The preprocessor handles the lines that begin with #, so #include <iostream> is replaced by the declarations of the stream types before the compiler proper reads your code. The compiler then translates the result into an object file, and the linker joins that file with the standard library to make an executable.

That is why one missing semicolon can produce a page of messages. The parser loses its place at the mistake, and every line after it is read against a structure that was already wrong; the later errors are consequences, not separate faults. Fix the first one and compile again before believing the rest.

Compiled once, then it is a program

A C++ program is not read by an interpreter while it runs. It is translated ahead of time into instructions for the machine it was built for, and the result is a file that starts without the compiler present. That is why a run here takes a moment on the first press: the translation happens on the server, and what follows is the program itself.

The same fact explains the shape of the errors. Everything the compiler can know from the text — a missing name, a wrong type, a call with the wrong number of arguments — is decided before a single line executes. A program that compiles can still be wrong, but it is wrong about what it does, not about what it says.

Three slips account for most first builds: a statement without its semicolon, a std::cout line without #include <iostream>, and a capital letter where the library expects lower case — Std::cout and Cout are different names, and neither one exists. Read the first error, fix that line, and run again before reading further.

Try these next

  • Print a short introduction with three lines: a name, a year, and a sentence that uses both.
  • Delete the #include <iostream> line, read the first error, then put it back and confirm the error is gone.
  • Change the year to a double and print it with a decimal point.

A three-line profile

Run it, then change the name and city and add a line with a total computed from two numbers. Predict the output before you press Run and check whether the compiler agrees.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Where does a C++ program begin?