C++ Get Started
Before you can write a single line of useful C++, you need two things: a way to turn your source code into a program the computer can run (a compiler), and a place to write that code (an editor or IDE). This lesson walks through setting up both, then writes, compiles, and runs a real C++ program — and explains exactly what every part of it means.
Overview: How C++ Programs Come to Life
C++ is a compiled language. Unlike Python or JavaScript, which are read and executed line-by-line by an interpreter, C++ source code must first be translated into machine code — raw instructions the CPU can execute directly. This translation is done by a program called a compiler.
The journey from the text you type to a running program has several stages:
- Preprocessing — lines starting with
#(like#include) are handled first. The preprocessor literally pastes in the contents of header files and expands macros, producing a single, larger block of code. - Compilation — the compiler translates the preprocessed C++ code into object code (machine instructions specific to your CPU architecture), stored in a
.oor.objfile. During this stage, the compiler checks your syntax and types, which is why C++ catches many mistakes before the program ever runs. - Linking — the linker combines your object code with code from libraries (such as the C++ Standard Library, which provides things like
std::cout) to produce a final, standalone executable file (e.g.a.outon Linux/macOS orprogram.exeon Windows). - Execution — the operating system loads the executable into memory and the CPU runs it, instruction by instruction.
Because this all happens ahead of time (“ahead-of-time compilation”), C++ programs tend to start instantly and run very fast — there’s no interpreter overhead at runtime. The tradeoff is that you must recompile every time you change your code, and the resulting executable is tied to a specific operating system and CPU architecture (a Windows .exe won’t run on Linux, for example).
What You Need Installed
To follow this course, you need a C++ compiler that supports at least C++17. The most common choices are:
| Compiler | Platform | How to get it |
|---|---|---|
| GCC / g++ | Linux, macOS, Windows (via MinGW or WSL) | Linux: usually pre-installed or sudo apt install g++. macOS: xcode-select --install. Windows: install MinGW-w64 or use WSL. |
| Clang | macOS, Linux, Windows | macOS ships Clang under the name g++/clang++ via Xcode tools. Linux: sudo apt install clang. |
| MSVC | Windows | Install Visual Studio with the “Desktop development with C++” workload. |
This course uses g++ (the GNU Compiler Collection’s C++ compiler) in examples, since it’s free, cross-platform, and the most widely used in tutorials. Any C++17-or-later compiler will work identically for the code shown here.
You’ll also want a text editor. A plain text editor works fine, but a code editor with C++ support — such as Visual Studio Code (with the C/C++ extension) — gives you syntax highlighting, autocomplete, and integrated error checking, which speeds up learning considerably. A full IDE like CLion or Visual Studio bundles the editor, compiler, and debugger together, which is convenient but not required.
You do not need to install anything special to check your setup — once g++ is installed, opening a terminal and typing g++ --version should print a version number.
Syntax: Compiling and Running from the Command Line
Regardless of which editor you use, you’ll eventually run a command like this to build your program:
g++ -std=c++17 -o program main.cpp
./program
g++— invokes the GNU C++ compiler.-std=c++17— tells the compiler which C++ standard version to use (C++17 in this case). Without this flag, some compilers default to an older standard.-o program— names the output executableprogram(on Windows this would typically beprogram.exe). If you omit-o, g++ names ita.outby default.main.cpp— the source file to compile. C++ source files conventionally use the.cppextension../program— runs the compiled executable (the./tells the shell to look in the current directory on Linux/macOS; on Windows you’d just typeprogramorprogram.exe).
If your editor or IDE has a “Run” button, it is doing exactly this under the hood: compiling your file, then executing the resulting binary.
Examples
Example 1: The Classic First Program
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Output:
Hello, World!
This is the smallest complete C++ program that does something visible. #include <iostream> pulls in the input/output library, which defines std::cout (“character output”). Every C++ program must have exactly one main function — this is where execution begins. Inside it, std::cout << "Hello, World!" << std::endl; sends the text to the console, followed by a newline (std::endl both prints a newline and flushes the output buffer). Finally, return 0; tells the operating system the program finished successfully — by convention, a return value of 0 means “no error,” while any nonzero value signals failure.
Example 2: Getting Input and Doing Something With It
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Hello, " << name << "! In 10 years you'll be "
<< (age + 10) << " years old." << std::endl;
return 0;
}
Output (given input Ada and 28):
Enter your name: Ada
Enter your age: 28
Hello, Ada! In 10 years you'll be 38 years old.
This example introduces std::cin (“character input”), which reads from the keyboard. The >> operator extracts a value into a variable, stopping at whitespace — so std::cin >> name reads one word into the std::string name, and std::cin >> age reads a number into the int age, automatically converting the typed text into an integer. Note that #include <string> is required to use std::string; forgetting it is a very common beginner error.
Example 3: A Small Calculation Program
#include <iostream>
int main() {
const double PI = 3.14159265;
double radius;
std::cout << "Enter the radius of a circle: ";
std::cin >> radius;
double area = PI * radius * radius;
double circumference = 2 * PI * radius;
std::cout << "Area: " << area << std::endl;
std::cout << "Circumference: " << circumference << std::endl;
return 0;
}
Output (given input 2):
Enter the radius of a circle: 2
Area: 12.5664
Circumference: 12.5664
This program declares a const double (a floating-point value that cannot be changed after initialization), reads a double from the user, and performs arithmetic to compute the area and circumference of a circle. It shows how variables, constants, and expressions combine in a real, if small, calculation — the same shape you’ll use in far larger programs.
Under the Hood: What Happens When You Build and Run
- You save your code in a file named
main.cpp. - You run
g++ -std=c++17 -o program main.cpp. The preprocessor expands#includedirectives, replacing#include <iostream>with the full declarations ofstd::cout,std::cin, and related tools. - The compiler checks every statement for correct syntax and type usage. If you misspell
std::coutor forget a semicolon, compilation stops here with an error — the executable is never produced. - Assuming no errors, the compiler emits object code, and the linker stitches it together with the pre-built Standard Library code to produce the executable
program. - Running
./programasks the operating system to load that executable into memory and start executing its instructions atmain. - The program runs top to bottom inside
main, executing each statement, until it hitsreturn 0;or the closing brace, at which point it exits and control returns to the shell.
Understanding this pipeline matters because C++ error messages often refer to a specific stage — a “syntax error” happens at compile time, while an “undefined reference” error happens at link time (usually meaning you forgot to include or link a needed piece of code), and a crash while the program is running is a runtime error.
Common Mistakes
Mistake 1: Forgetting std:: or a using declaration
#include <iostream>
int main() {
cout << "Hi!" << endl; // error: 'cout' was not declared
return 0;
}
cout and endl live inside the std namespace, so the compiler doesn’t recognize bare cout. Fix it by qualifying the name (std::cout) or by adding using namespace std; after the includes:
#include <iostream>
using namespace std;
int main() {
cout << "Hi!" << endl;
return 0;
}
This course generally prefers writing std:: explicitly, since using namespace std; can cause name clashes in larger programs — but you’ll see both styles in the wild.
Mistake 2: Missing Semicolons or Mismatched Braces
#include <iostream>
int main() {
std::cout << "Oops" << std::endl
return 0;
}
The missing semicolon after std::endl causes a compile error (often reported on the next line, which can be confusing for beginners). Every statement in C++ must end with a semicolon:
#include <iostream>
int main() {
std::cout << "Oops" << std::endl;
return 0;
}
Best Practices
- Always specify a language standard explicitly (e.g.
-std=c++17) so behavior doesn’t silently change between compiler versions. - Enable warnings while learning: compile with
-Wall -Wextrato catch subtle bugs the compiler would otherwise stay quiet about. - Name your main source file something meaningful, like
main.cpp, and keep one logical program per file while you’re starting out. - Read compiler error messages from the first error down — a single mistake (like a missing semicolon) can cascade into dozens of confusing follow-on errors.
- Get comfortable with the command line early, even if you use an IDE — it makes error messages easier to understand and is essential once you move to real projects.
Practice Exercises
- Exercise 1: Install a C++ compiler on your machine and confirm it works by compiling and running Example 1 above. Then modify the message it prints and rebuild it.
- Exercise 2: Write a program that asks the user for two integers and prints their sum, difference, and product, each on its own line.
- Exercise 3: Take Example 3 (the circle program) and extend it to also compute and print the diameter of the circle.
Summary
- C++ is a compiled language: source code goes through preprocessing, compilation, and linking before it becomes a runnable executable.
- You need a compiler (g++, Clang, or MSVC) and an editor or IDE to write C++; this course uses g++ with the C++17 standard.
- Every C++ program has exactly one
mainfunction, which is where execution starts and (usually) ends withreturn 0;. #includepulls in library declarations;std::cout/std::cinhandle console output and input.- Compiler errors happen before your program ever runs — reading them from the first one down is the fastest way to fix them.
