Introduction
C++ remains an essential language in 2026 for systems, games, and high-performance applications. This tutorial guides you step by step from installation to creating simple functions. Each concept comes with a complete, compilable example. You will learn to write clean code and avoid classic mistakes right from the beginning.
Prerequisites
- Computer running Windows, macOS, or Linux
- Basic computer knowledge
- Terminal or command prompt
Installing the Compiler
# Linux (Ubuntu/Debian)
sudo apt update && sudo apt install g++
# macOS
xcode-select --install
# Windows : installer MinGW ou MSYS2This command installs the g++ compiler required to convert C++ code into an executable. Verify the installation with g++ --version before proceeding.
First Hello World Program
#include <iostream>
int main() {
std::cout << "Hello, C++ 2026 !" << std::endl;
return 0;
}This file contains the minimal program. Compile it with g++ main.cpp -o hello and run ./hello to see the message displayed.
Declaring Variables
#include <iostream>
#include <string>
int main() {
int age = 25;
double prix = 19.99;
std::string nom = "Alice";
std::cout << nom << " a " << age << " ans." << std::endl;
return 0;
}The code declares and uses three basic types. Compile and run it to observe the formatted output. Avoid using overly short variable names.
Using Loops
#include <iostream>
int main() {
for(int i = 1; i <= 5; i++) {
std::cout << "Itération : " << i << std::endl;
}
return 0;
}The for loop displays 5 iterations. Experiment by modifying the condition to understand its behavior.
Creating a Function
#include <iostream>
int addition(int a, int b) {
return a + b;
}
int main() {
int resultat = addition(8, 12);
std::cout << "Résultat : " << resultat << std::endl;
return 0;
}The addition function takes two parameters and returns their sum. Call it from main to easily reuse the code.
Best Practices
- Always initialize variables
- Use descriptive names
- Compile with the -Wall flag to see warnings
- Separate declarations and implementations in larger projects
Common Mistakes to Avoid
- Forgetting the semicolon at the end of statements
- Missing necessary header includes
- Exceeding array bounds
- Ignoring compiler warning messages
Further Learning
Deepen your skills with our complete C++ courses.