Anatomy of a C++ Program: main, Includes, Namespaces
A C++ program file is just a text file with a specific structure. Let’s start with a simple C++ program.
If you press the “Run” button, the program will compile and run. It will print the line Hello, world. and exit.
This code skeleton will appear throughout the course. Let’s walk through it one line at a time.
Line 1: #include <iostream>
Your program can use code that other people have already written and tested.
Before your program compiles, a tool called the preprocessor handles lines that start with #. These lines are called directives because they are instructions for the preprocessor.
On line 1, #include <iostream> tells the preprocessor: go find the file named iostream and paste its contents into this program right here, in place of this line. This file contains the code for printing to the screen and reading from the keyboard.
iostreamis part of the C++ standard library, which is a collection of prewritten code that comes with every C++ compiler.
The angle brackets < > tell the preprocessor to look for the file in the standard library folder. Files you include this way are called header files.
Lines 3 & 6: int main() { }
A function is just a block of code that has a name. Every C++ program has exactly one function named main. This is where execution begins. When you run the program, the computer starts at the first line inside main and works its way down.
Here is what each part of int main() { } means:
- The word
intbeforemainmeans this function will hand back a whole number when it finishes. That number is called an exit code, and it tells the operating system whether the program finished successfully. - The parentheses
()aftermainare where a function’s inputs (or parameters) would go. For now,maindoesn’t need any inputs, so the parentheses are empty. - The code between the curly braces
{ }is called the body ofmain.
Line 4: std::cout and printing
Inside main, we have:
std::cout << "Hello, world." << std::endl;You’ll notice the std:: prefix before cout. We’ll come back to that shortly.
coutis short for “character output”. It’s an object that represents the screen’s output. You send text to it using the<<operator, which you can think of as an arrow pointing toward the screen. The text inside double quotes" "is printed exactly as written.std::endlstands for “end line”. It tells the program to start the next output on a fresh line below it.- The semicolon
;at the end marks the end of the statement. A statement is one complete step the program carries out, and every statement in C++ must end with a semicolon.
What is std:: doing there?
The standard library defines thousands of names like cout, sort, and string. A namespace is a container for names like that. To keep those names from clashing with names you come up with, everything in the standard library is placed in a namespace called std.
The std:: prefix means the following name belongs to the std namespace. So std::cout specifically means the cout from the standard library, not some other cout you might have defined yourself.
The shortcut: Many examples online skip the prefix entirely by adding this line near the top:
using namespace std;With that in place, you can write
coutinstead ofstd::cout. That’s fine for small programs, but it can cause hard-to-diagnose problems in larger projects.
The examples in this course write std:: explicitly so you understand where each name comes from.
Line 5: return 0;
The last line hands a number back to the operating system. return 0 means the program finished without errors. If something went wrong, the program could return a different number instead, such as return 1. The operating system would then know something failed.
For now, we’ll always write return 0. The int before main was a promise to return a whole number, and this is where it delivers.
Common mistakes
Here are a few things that are easy to get wrong as a beginner.
| Mistake | What happens |
|---|---|
| Forgetting semicolons | Every statement inside a function ends with ;. The #include line doesn’t because it’s not a statement, but an instruction for the preprocessor. |
Mistyping main | The compiler looks for the exact name main. If you accidentally write Main or mian, the code will compile but won’t produce a working program. |