Install a compiler, write "Hello, World", and understand the compile → link → run cycle. Print to the console and read input with iostream.
C++ is a compiled language: source code becomes a native executable before it runs. Install a toolchain (g++/clang on macOS/Linux, MSVC or MinGW on Windows), then compile with a single command. -std=c++20 selects the language standard; -Wall turns on warnings you want to see.
# install a compiler
# macOS: xcode-select --install (clang)
# Ubuntu: sudo apt install g++
# Windows: install MSVC or MinGW-w64
# compile hello.cpp into an executable named "hello"
g++ -std=c++20 -Wall -Wextra hello.cpp -o hello
# run it
./hello # on Windows: hello.exeEvery C++ program starts at main(), which returns an int exit code (0 means success). #include pulls in a library header; <iostream> gives you std::cout for output. << streams values out, and std::endl writes a newline and flushes.
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0; // 0 = success
}std::cin reads from the keyboard into a variable of the right type — the >> operator parses text into an int here. Printing mixes literals and variables on one line. This is the whole read → compute → print loop that most programs are built from.
#include <iostream>
int main() {
std::cout << "How old are you? ";
int age;
std::cin >> age; // read an int from stdin
std::cout << "Next year you'll be "
<< age + 1 << ".\n"; // \n is a newline (no flush)
return 0;
}The build is four stages. Understanding them makes compiler and linker errors readable: a "syntax error" is the compiler; an "undefined reference" is the linker not finding a definition.
hello.cpp
│
▼ (1) PREPROCESS — expand #include and #define into one big file
translation unit
│
▼ (2) COMPILE — check syntax/types, emit object code (hello.o)
hello.o
│
▼ (3) LINK — join your .o files + libraries into one binary
hello (executable)
│
▼ (4) RUN — the OS loads it; execution begins at main()