C++ for Beginners Part 1: Your First C++ Program
What is C++, why it still matters in 2024, and how to write, compile, and run your very first program. Covers environment setup with g++ and VS Code, the anatomy of a minimal program, and how the compiler turns source code into an executable.
C++ for Beginners: A Complete Guide
An eight-part beginner-friendly journey through C++: from writing your first program and understanding variables, through control flow, functions, and arrays, to the heart of C++ — pointers, classes, inheritance, and polymorphism. Each article is self-contained and builds on the previous, giving you both a quick reference and a progressive path to mastery.
- 1 C++ for Beginners Part 1: Your First C++ Program
- 2 C++ for Beginners Part 2: Variables, Data Types, and Operators
- 3 C++ for Beginners Part 3: Control Flow — Conditions and Loops
- 4 C++ for Beginners Part 4: Functions
- 5 C++ for Beginners Part 5: Arrays, Strings, and std::vector
Table of Contents
- Why learn C++?
- Setting up your environment
- Option 1: Linux / macOS (recommended)
- Option 2: Windows
- Editor: VS Code
- Hello, World
- Anatomy of the program
- #include <iostream>
- int main()
- std::cout
- return 0;
- What the compiler does
- Useful compiler flags
- Common errors beginners hit
- Forgot the semicolon
- Forgot #include
- Used cout without std::
- Key takeaways
Why learn C++?
C++ is almost 45 years old and still ranked among the top five most-used languages worldwide. You'll find it inside:
- Operating systems — Windows, macOS, and Linux kernels are partly written in C/C++
- Game engines — Unreal Engine is C++; Unity's runtime is C/C++
- Embedded systems — microcontrollers, automotive ECUs, medical devices
- High-frequency trading — microsecond latency matters
- Compilers and databases — LLVM, MySQL, PostgreSQL, and Chrome are C++
If you want to understand how computers actually work — memory, performance, hardware — C++ is the clearest window into that world.
Setting up your environment
Option 1: Linux / macOS (recommended)
On macOS, install the Xcode Command Line Tools:
xcode-select --install
On Ubuntu/Debian:
sudo apt update && sudo apt install build-essential
Verify the install:
g++ --version
# g++ (Ubuntu 13.2.0) 13.2.0
Option 2: Windows
Install MSYS2 from msys2.org, then inside the MSYS2 terminal:
pacman -S mingw-w64-ucrt-x86_64-gcc
Editor: VS Code
Download VS Code and install the C/C++ extension by Microsoft. It gives you syntax highlighting, IntelliSense, and a built-in debugger.
Hello, World
Create a new file called hello.cpp and type the following:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Save the file, open a terminal in the same directory, and compile:
g++ -o hello hello.cpp
Run it:
./hello
# Hello, World!
On Windows:
hello.exe
# Hello, World!
Anatomy of the program
Let's go line by line.
#include <iostream>
This is a preprocessor directive. It tells the compiler to include the iostream (input/output stream) header from the C++ Standard Library. Without it, std::cout doesn't exist.
#include <iostream> // I want to use cout and cin
The #include is processed before compilation. Think of it as copy-pasting the header file's contents at that line.
int main()
Every C++ program has exactly one main function. It's the entry point — where execution begins. int means the function returns an integer to the operating system.
int main() {
// program starts here
}
std::cout
cout stands for "character output". It's defined in the std (standard) namespace. The << operator sends data to the output stream.
std::cout << "Hello, World!" << std::endl;
std::endl flushes the output buffer and moves the cursor to a new line. You can also use ' ' which is slightly faster (it doesn't flush):
std::cout << "Hello, World!
";
return 0;
The main function returns 0 to signal success to the operating system. Any non-zero value means something went wrong. Modern C++ actually makes this optional — the compiler adds it automatically — but it's good practice to be explicit.
What the compiler does
When you run g++ -o hello hello.cpp, four things happen:
- Preprocessing —
#includedirectives are expanded, macros replaced - Compilation — C++ source is translated to assembly
- Assembly — assembly is turned into machine code (an object file
.o) - Linking — object files and standard library code are combined into one executable
You can see the intermediate steps:
g++ -E hello.cpp -o hello.i # Stop after preprocessing
g++ -S hello.cpp -o hello.s # Stop after compilation (assembly output)
g++ -c hello.cpp -o hello.o # Stop after assembly (object file)
g++ hello.o -o hello # Link
In practice you just run g++ -o hello hello.cpp — one command, all four steps.
Useful compiler flags
# Strict warnings (recommended from day one)
g++ -Wall -Wextra -o hello hello.cpp
# C++17 standard (use modern features)
g++ -std=c++17 -Wall -Wextra -o hello hello.cpp
# Debug build (includes debug symbols for gdb)
g++ -std=c++17 -g -o hello hello.cpp
# Optimised release build
g++ -std=c++17 -O2 -o hello hello.cpp
Add -std=c++17 -Wall -Wextra to every compile command from now on. The warnings will save you hours.
Common errors beginners hit
Forgot the semicolon
std::cout << "Hello" // ← missing ;
// error: expected ';' before 'return'
Every statement in C++ ends with a semicolon.
Forgot #include
int main() {
std::cout << "Hello
"; // error: 'cout' is not a member of 'std'
}
Fix: add #include <iostream> at the top.
Used cout without std::
cout << "Hello
"; // error: 'cout' was not declared in this scope
Either write std::cout or add using namespace std; after your includes (though std:: is generally preferred in real code — it makes it clear where things come from).
Key takeaways
- C++ compiles to native machine code — that's why it's fast.
- Every program starts at
main(). std::coutprints to the terminal;<<is the insertion operator.- Compile with
-std=c++17 -Wall -Wextrafrom the start. - The compiler works in four stages: preprocess → compile → assemble → link.
Next up: variables, data types, and operators — the building blocks of every C++ program.