C++ Programming Tutorial

Learn modern C++ step-by-step — from basics to advanced features like templates, STL, and smart pointers. Clear explanations with practical, runnable examples.

Getting Started with C++

To write and run C++ programs, you need a compiler that translates your source code into machine-executable binaries. This guide covers setting up your development environment on all major platforms and running your first program.

Choosing a Compiler

C++ has several high-quality compilers available for free:

CompilerPlatformCommandNotes
GCC (g++)Linux, macOS, Windows (MinGW)g++Most widely used, excellent standard compliance
ClangmacOS, Linux, Windowsclang++Fast compilation, great error messages
MSVCWindowsclIntegrated with Visual Studio, best Windows support

Installation by Platform

Linux (Ubuntu/Debian)

Terminal
# Install GCC, G++, and build tools
sudo apt update
sudo apt install build-essential

# Verify installation
g++ --version
# g++ (Ubuntu 13.2.0) 13.2.0

macOS

Terminal
# Install Xcode Command Line Tools (includes Clang)
xcode-select --install

# Verify
clang++ --version
# Apple clang version 15.0.0

# Or install GCC via Homebrew
brew install gcc

Windows

Options
# Option 1: Install Visual Studio Community (free)
# Download from visualstudio.microsoft.com
# Select "Desktop development with C++" workload

# Option 2: Install MinGW-w64 (GCC for Windows)
# Download from winlibs.com or use MSYS2:
pacman -S mingw-w64-ucrt-x86_64-gcc

# Option 3: Use WSL (Windows Subsystem for Linux)
wsl --install
# Then follow Linux instructions inside WSL

Your First Program

main.cpp
#include <iostream>

int main() {
    std::cout << "C++ is ready!" << std::endl;
    return 0;
}

Compiling and Running

Terminal
# Compile with C++17 standard and warnings enabled
g++ -std=c++17 -O2 -Wall -Wextra main.cpp -o main

# Run the program
./main
# Output: C++ is ready!

# Flags explained:
# -std=c++17   Use C++17 standard
# -O2          Optimization level 2 (fast code)
# -Wall        Enable all common warnings
# -Wextra      Enable extra warnings
# -o main      Name the output binary "main"

Try C++ Online (No Installation)

If you want to experiment without installing anything, these online compilers let you write and run C++ in your browser:

  • Compiler Explorer (godbolt.org): See generated assembly alongside your code — great for learning.
  • Wandbox: Supports multiple compilers and C++ versions.
  • OnlineGDB: Full IDE with debugger in the browser.
  • Replit: Collaborative coding environment with C++ support.

Recommended Code Editors and IDEs

  • VS Code + C/C++ extension: Lightweight, fast, excellent IntelliSense. Free.
  • CLion (JetBrains): Full-featured IDE with CMake integration. Paid (free for students).
  • Visual Studio: Best Windows IDE for C++. Community edition is free.
  • Qt Creator: Excellent for Qt/GUI development. Free and open source.

Project Structure for Beginners

Typical layout
my_project/
├── main.cpp          # Entry point
├── utils.h           # Header: function declarations
├── utils.cpp         # Source: function implementations
└── Makefile          # Build instructions (optional)

# As projects grow, use CMake:
# CMakeLists.txt for portable build configuration
  • Start with a single .cpp file for learning.
  • Separate declarations (headers .h) from implementations (.cpp) as code grows.
  • Use a build system (Make or CMake) once you have multiple source files.
  • Always compile with warnings enabled to catch issues early.

Keep Practicing

Use the online compiler to run every example and experiment with modifications. The best way to learn C++ is by writing code — even small programs build strong foundations.