When you first start to learn a programming language, you will most likely begin by displaying “Hello World” in the output of the program. Most resources show the code but do not break down step by step why each component is needed. In this tutorial we will look at every single line of a simple C++ program and understand exactly why it is there.
The Complete Program
// My first C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "This is my first C++ program\n";
return 0;
}
Now let’s break down each line.
Line 1: The Comment
// My first C++ Program
Two forward slashes indicate a single-line comment written by the programmer. Comments are completely ignored by the compiler — they do not affect how the program runs. Programmers use them to explain what the code is doing or to leave notes for themselves and others.
You can also write a multi-line comment using block syntax:
/*
This is my first C++ program
It prints a message to the screen
*/
Anything between /* and */ is treated as a comment and ignored by the compiler.
Line 2: The Include Directive
#include <iostream>
Lines starting with a hash sign (#) are called preprocessor directives. This line tells the compiler to include the iostream header file before compiling. Without it, we cannot perform standard input and output operations — such as printing text to the screen using cout. Think of it as importing a toolbox that gives you access to input/output tools.
Line 3: Using Namespace std
using namespace std;
This is an important line because it links the standard C++ library into your program. As code becomes more complex, different libraries can have functions, classes, and variables with the same name. The using namespace std directive tells the compiler to use the standard namespace by default, preventing it from confusing identically named items from different libraries.
Without this line, you would need to write std::cout instead of just cout every time.
Line 5: The Main Function
int main()
This is the entry point of every C++ program. Without it, the program will not start — it is like trying to start a car without a battery. No matter where int main() appears in the code, the execution of the program always begins here.
The int at the front means the function returns an integer value when it finishes. This return value tells the operating system whether the program ended successfully.
Line 6: The Opening Brace
{
The opening curly brace marks the start of the main function body. Everything written between { and } is the code that will be executed when the program runs. This is where you implement what you want the program to do.
Line 7: Printing to the Screen
cout << "This is my first C++ program\n";
This is the body of the program. cout stands for “character output” and is used to display text on the screen. The << operator sends the string to the output.
The \n at the end is an escape character that means “new line” — it moves the cursor to the next line after the text is displayed. You could also use endl to achieve the same result:
cout << "This is my first C++ program" << endl;
Note the semicolon at the end — every statement in C++ must end with a ; or the program will not compile.
Line 8: Return Statement
return 0;
This line terminates the main function and returns the value 0 to the operating system. On most operating systems such as Windows and macOS, a return value of 0 signals that the program ended normally without errors. Any non-zero return value typically indicates that something went wrong.
Line 9: The Closing Brace
}
The closing curly brace marks the end of the main function body. Every opening { must have a matching closing }. After the closing brace, the program ends.
Putting It All Together
Here is the full program again with inline comments explaining each part:
// My first C++ Program <- comment, ignored by compiler
#include <iostream> <- include input/output tools
using namespace std; <- use standard library by default
int main() <- entry point of the program
{ <- start of main function body
cout << "This is my first C++ program\n"; <- print to screen
return 0; <- exit successfully
} <- end of main function body
Summary
Every line in a C++ program has a purpose. Even the simplest Hello World program introduces the core building blocks you will use in every C++ program you ever write — includes, namespaces, the main function, output statements, and return values.
| Line | Purpose |
|---|---|
// comment | Notes for the programmer, ignored by compiler |
#include <iostream> | Imports input/output functionality |
using namespace std; | Avoids needing to prefix standard library calls with std:: |
int main() | Entry point — program starts here |
{ | Opens the function body |
cout << | Prints text to the screen |
return 0; | Signals successful program exit |
} | Closes the function body |
Ready to go beyond Hello World? C++ Better Explained takes you from the basics all the way through classes, pointers, and real-world projects — grab the book and start building today.