If you have used Python, you are used to typing python3 game.py and watching it run straight away. Python reads your code and carries it out line by line while the program runs.
C++ works differently. Before your program runs at all, a program called a compiler (a translator that turns source code into machine code) reads the whole file, checks it, and produces a separate executable (a file of machine instructions your processor can run directly). You then run that executable. Translating once, up front, is one reason C++ programs are so fast: by the time they run, there is nothing left to translate.
It is like the difference between a live interpreter at a meeting and a book that has already been translated. The translated book takes work to produce, but reading it afterwards is quick.
Explained simply
Here is the smallest useful C++ program. Save it in a file called hello.cpp.
#include <iostream> copies in the standard library's input/output stream tools. Without it, the compiler has never heard of std::cout.
int main() is the main function: the place where every C++ program starts. The int says it hands back a whole number to the operating system when it finishes.
The curly braces { and } mark the start and end of the function's body.
std::cout is the standard output stream: usually the terminal window. The << operator pushes text into it.
std:: means "from the standard library". The standard library keeps its names inside a namespace (a named group that stops names clashing) called std.
"\n" is a newline character, so the cursor moves to the next line.
Watch it explained
C++ Dev Env Setup - 2 - clang Hello World application from linux terminal
memleaks
C++ Hello World Project with Clang CMake and Ninja
The first command runs the compiler clang++. The flags (options that start with a dash) mean:
Flag
Meaning
-std=c++17
Use the C++17 version of the language
-Wall -Wextra
Turn on lots of helpful warnings
-o hello
Call the output file hello
The second command runs the executable. On Windows the file would be hello.exe. If you use g++ instead of clang++, the same flags work.
The build pipeline: the source file hello.cpp goes into the compiler clang++, which checks it and produces an executable file of machine code; running the executable prints output in the terminal
Behind the scenes the compiler does a few jobs in a row. The preprocessor handles lines starting with # (like #include). The compiler proper turns your code into machine code. The linker joins your code with the parts of the standard library you used. The one clang++ command runs all three steps for you.
Worked example
Compilers are strict, and that is a gift: they catch many mistakes before your program ever runs. Here is the same program with the semicolon missing after the text:
Read it from left to right. hello.cpp:4:35 means file hello.cpp, line 4, column 35. Then comes the kind of problem (error) and a description. The compiler even draws a caret ^ where it expected the semicolon and suggests the fix. No executable is made until every error is gone.
This is how clang words it. Other compilers, such as g++ and Microsoft's MSVC, word their messages differently and lay them out differently, but they all give the file, the line and a description of the problem, so read them the same way.
Typing errors are just as easy to read. If you declare int score = 10; and later write scor, clang says:
Output
error: use of undeclared identifier 'scor'; did you mean 'score'?
Warnings are different from errors: the program still compiles, but the compiler thinks something is probably wrong. A good habit from day one is to fix every warning. Professional teams often add -Werror, which turns warnings into errors so they cannot be ignored.
Common mistakes
Forgetting to recompile. You edit hello.cpp, run ./hello again, and nothing changes. The executable is the old translation. Compile again after every change.
Fixing the last error first. One early mistake, such as a missing brace, can confuse the compiler and cause a pile of later errors. Fix the first error, recompile, and repeat.
Writing using namespace std; everywhere. You will see it in many tutorials. It saves typing std:: but pulls hundreds of names into your program, which can clash with your own (for example, a variable called count or size). This course writes std:: in full, which is what most professional code does.
Mixing up << and >>. For output, the arrows point into std::cout: the text flows into the stream.
Where you see this in real life
C++ is the language behind much of the software where speed matters: game engines such as Unreal Engine, web browsers such as Chrome and Firefox, databases, trading systems, music and video software, and the code that runs on robots and cars. Many tools you use every day, including the Python interpreter's fast libraries, have C or C++ underneath. All of them are built exactly as you just did: source files go in, a compiler and linker run, and an executable comes out.
Your turn
Write a program that prints your name on one line and your favourite food on the next, using a single std::cout statement.
Solution. You can chain several << in one statement: