What C++ is
C++ is a compiled language that gives you direct control over memory and hardware, with a large standard library layered on top so that everyday code can still be written at a high level. It has no garbage collector and no virtual machine: a C++ program becomes machine code and runs as fast as the computer allows. Writing it feels precise and demanding — the compiler checks types strictly, but mistakes with memory are not caught for you, and part of learning the language is learning the habits that avoid them.
Where C++ is used
- Games and game engines
- The major game engines, and the graphics-heavy games built on them, are written in C++ because frame-by-frame rendering and physics need predictable speed and control over memory.
- Browsers, operating systems and compilers
- The JavaScript engines inside web browsers, large parts of the browsers themselves, database engines and many compilers are C++.
- Embedded and automotive systems
- Code that runs on a car's control unit, a medical device or a router is often C++ (or C), where memory is scarce and timing matters.
- High-performance computing and finance
- Simulations, trading systems and the numerical libraries that other languages call into — including much of what runs underneath Python's data tools — are C++ where speed is the point.
- Desktop applications
- Long-lived desktop software such as image editors, audio workstations and CAD tools is commonly C++, usually with a cross-platform GUI toolkit.
Your first C++ program
Saved as hello.cpp. You can paste it straight into the playground to see it run.
#include <iostream>
int main() {
int scores[] = {72, 88, 95};
int total = scores[0] + scores[1] + scores[2];
std::cout << "Total: " << total << "\n";
std::cout << "Average: " << total / 3 << "\n";
return 0;
}What it prints
Total: 255
Average: 85- Line 1 includes the input and output part of the standard library. Without it
std::coutdoes not exist, and the compiler will say so. - Line 3 begins
main, the function every C++ program starts in. It returns anintto the operating system, and the braces hold the program. - Line 4 creates an array of three
intvalues. The type isint, the square brackets say array, and the braces list the contents; positions start at 0. - Line 5 adds the three items into a new
intcalledtotal. Every variable has a declared type, and each statement ends with a semicolon. - Lines 6 and 7 print.
std::coutis the output stream,<<sends each piece to it in turn, and "\n" ends the line. Becausetotaland 3 are both integers,total / 3is whole-number division: 255 / 3 is 85, and 256 / 3 would also print 85. - Line 8 returns 0, the conventional signal that the program finished without error. Line 9 closes
main.
Run C++ on your own computer
There is no single official C++ installer: the language is a standard, and you pick a compiler that implements it. The three mainstream ones — GCC (g++), Clang (clang++) and Microsoft's MSVC — all handle everything in this guide, so choose whichever is easiest to install on your system.
Install a compiler
On Linux, install your distribution's build tools (for example the build-essential package on Debian and Ubuntu, which includes g++). On macOS, run xcode-select --install in Terminal to get Clang. On Windows, either install Visual Studio Community or its Build Tools with the C++ workload for MSVC, or install MinGW-w64 through MSYS2 to get g++.
Check it works
Use clang++ --version on macOS, or cl in a Visual Studio developer prompt. Any compiler released in the last few years supports the C++17 and C++20 features a beginner meets.
Shellg++ --versionCompile the program
Save the code as hello.cpp and compile it into an executable called hello. -Wall and -Wextra turn on warnings, which you should read: they catch many mistakes the language itself lets through. With MSVC the equivalent is cl /EHsc /W4 /std:c++20 hello.cpp.
Shellg++ -std=c++20 -Wall -Wextra hello.cpp -o helloRun the executable
On Windows type hello or .\hello instead. Unlike Python or JavaScript, the source file is not what runs; the compiled program is, so after every change you compile again.
Shell./helloUse an editor that knows C++
An editor with C++ support and a debugger will show errors inline and let you step through code a line at a time. That matters more in C++ than in most languages, because a memory mistake often shows up far from where it was made.
A learning order for C++
Stages, not a timetable. Each one exists because the next would not make sense without it, and how long each takes depends on how much you write.
Stage 1. Compiling, output and variables
- the compile-and-run cycle
- #include and main
- std::cout and std::cin
- int, double, char and bool
- std::string
- arithmetic and integer division
C++ has a real build step and strict types from the first line. Getting the compiler installed and understanding what a type is are worth doing slowly before anything else.
Stage 2. Control flow and functions
- if / else and switch
- for, while and do-while
- functions, parameters and return values
- pass by value and pass by reference
- scope and const
- reading compiler errors and warnings
Functions in C++ introduce the difference between copying a value and referring to it, an idea that runs through the whole language. Reading warnings carefully is a habit best formed here.
Stage 3. The standard library
- std::vector
- range-based for loops
- std::string in depth
- std::array and std::map
- algorithms such as sort and find
- iterators
Modern C++ leans on std::vector and the algorithms rather than raw arrays and hand-written loops. Learning the library early means writing safer code from the start.
Stage 4. Classes and structure
- struct and class
- constructors and destructors
- member functions and this
- const correctness
- header and source files
- operator overloading basics
Classes are where C++ turns data and behaviour into a single unit, and destructors are the foundation of how the language manages resources without a garbage collector.
Stage 5. Memory and ownership
- stack versus heap
- pointers and references
- new and delete, and why to avoid them
- std::unique_ptr and std::shared_ptr
- RAII
- undefined behaviour
This is the stage that makes C++ different from garbage-collected languages. Owning memory explicitly is what gives the language its speed, and smart pointers are what make it manageable.
Stage 6. Templates, build systems and beyond
- function and class templates
- inheritance and virtual functions
- exceptions
- CMake
- debuggers and sanitizers
- a project of your own
Templates power the whole standard library, and a build system becomes unavoidable once a project has more than a few files. Sanitizers are the tool that turns silent memory bugs into loud ones.
Mistakes beginners make in C++
- Forgetting std:: or the #include
- error: 'cout' was not declared in this scope; did you mean 'std::cout'? means the name is missing its namespace; error: 'cout' is not a member of 'std' means the <iostream> include is missing. The standard library lives in the std namespace, and each part of it needs its own header.
- Reading a variable that was never set, or an array position that does not exist
- int total; followed by total += x, or scores[3] on a three-element array, compiles without complaint and is undefined behaviour: the program may print a random number, crash, or appear to work until one day it does not. Initialise every variable when you declare it, prefer std::vector with .at() while learning, and compile with -fsanitize=address,undefined so these mistakes crash immediately instead.
- Writing = where == was meant
- if (count = 0) compiles, sets count to zero and takes the else branch every time. With -Wall the compiler prints warning: suggest parentheses around assignment used as truth value; without warnings turned on, it says nothing. Compile with warnings and treat them as errors while learning.
- A linker error rather than a compiler error
- undefined reference to `greet()` (or LNK2019 unresolved external symbol with MSVC) appears after the compiler has finished: a function was declared, or its header included, but the file that defines it was never compiled or never listed on the command line. Add the missing .cpp file to the command, or check that the definition's name and parameter types match the declaration.
- Missing the semicolon after a class
- error: expected ';' after class definition has an exact fix: a class or struct body ends with a closing brace and a semicolon, unlike a function body. Leaving it out also produces confusing errors on the lines that follow, so look here first when a class is followed by nonsense.
Strengths and trade-offs
Where it is strong
- Speed and predictability: no garbage-collector pauses, no interpreter, and control over exactly where every byte lives, which is why it is chosen for engines, browsers and trading systems.
- It runs everywhere, from microcontrollers with a few kilobytes of memory to supercomputers, with the same language.
- The standard library and modern features — smart pointers, std::vector, lambdas, ranges — let most day-to-day code be written safely and at a high level.
- What you learn about memory, types and how a program is actually built transfers to every other language, and makes their abstractions easier to see through.
Where it is not
- It is a large and old language with several eras of style layered on top of one another, and much material online teaches the older, more dangerous habits.
- Mistakes with memory are not caught for you. Undefined behaviour means a bug can go unnoticed, corrupt data silently or appear only on someone else's machine.
- Setup is harder than for most languages: no single installer, a separate compile and link step, and a build system to learn once a project has more than a few files.
- Compile times on large projects are long, and template errors can run to dozens of lines. For everyday scripting, web work or data analysis, Python or JavaScript will get you there faster.
Who C++ is for
C++ is the right language if you want to work on games, engines, embedded systems, high-performance software or anything where you need to know exactly what the machine is doing, and it is a strong choice if your course teaches it. It is also a valuable second or third language for a programmer who wants to understand what garbage collectors and interpreters have been doing on their behalf. As a first language it is harder than Python or JavaScript: progress is slower at the start and the errors are less friendly. If your goal is web pages, data analysis or a quick working prototype, start elsewhere and come back. If you want systems programming with memory safety enforced by the compiler, Rust is the modern alternative worth comparing.
Questions about learning C++
- Should I learn C before C++?
- No. C++ contains most of C, so you will meet the C parts as you go, but starting with modern C++ — std::string, std::vector and references — is safer and faster than starting with raw pointers and character arrays. Learn C separately later if you head towards operating systems or embedded work, where it is still the main language.
- Is C++ too hard for a first language?
- It is harder than Python or JavaScript, not impossible. The difficulty is not the syntax but that the language expects you to manage memory correctly and does not always tell you when you have not. Plenty of university courses start with it, and students who get through the early stages tend to find other languages easy afterwards. If you have a free choice and no particular reason to start here, start with Python and come to C++ second.
- Which compiler should I use?
- For learning it does not matter. GCC and Clang are free, open source and available on every platform; MSVC is the natural choice inside Visual Studio on Windows. They differ in the wording of error messages and a few flags, not in the language, and switching later is easy. Use whichever your operating system makes simplest to install.
- What does C++20 or C++23 mean?
- The language is defined by an international standard that is revised every three years, and each revision is named for its year: C++11, 14, 17, 20, 23. Newer standards add features but rarely remove them, so code written for an older one still compiles. Telling the compiler which standard to use with -std=c++20 gives you the modern features; everything in this guide works with C++17 or later.
The primary source
When this guide and the official documentation disagree, the documentation is right and we would like to hear about it.