Tutorial

C++ for Beginners Part 1: Your First C++ Program

What is C++, why it still matters in 2024, and how to write, compile, and run your very first program. Covers environment setup with g++ and VS Code, the anatomy of a minimal program, and how the compiler turns source code into an executable.


SERIES

C++ for Beginners: A Complete Guide

An eight-part beginner-friendly journey through C++: from writing your first program and understanding variables, through control flow, functions, and arrays, to the heart of C++ — pointers, classes, inheritance, and polymorphism. Each article is self-contained and builds on the previous, giving you both a quick reference and a progressive path to mastery.

Table of Contents

Why learn C++?

C++ is almost 45 years old and still ranked among the top five most-used languages worldwide. You'll find it inside:

  • Operating systems — Windows, macOS, and Linux kernels are partly written in C/C++
  • Game engines — Unreal Engine is C++; Unity's runtime is C/C++
  • Embedded systems — microcontrollers, automotive ECUs, medical devices
  • High-frequency trading — microsecond latency matters
  • Compilers and databases — LLVM, MySQL, PostgreSQL, and Chrome are C++

If you want to understand how computers actually work — memory, performance, hardware — C++ is the clearest window into that world.


Setting up your environment

On macOS, install the Xcode Command Line Tools:

Bash
xcode-select --install

On Ubuntu/Debian:

Bash
sudo apt update && sudo apt install build-essential

Verify the install:

Bash
g++ --version
# g++ (Ubuntu 13.2.0) 13.2.0

Option 2: Windows

Install MSYS2 from msys2.org, then inside the MSYS2 terminal:

Bash
pacman -S mingw-w64-ucrt-x86_64-gcc

Editor: VS Code

Download VS Code and install the C/C++ extension by Microsoft. It gives you syntax highlighting, IntelliSense, and a built-in debugger.


Hello, World

Create a new file called hello.cpp and type the following:

C++
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Save the file, open a terminal in the same directory, and compile:

Bash
g++ -o hello hello.cpp

Run it:

Bash
./hello
# Hello, World!

On Windows:

Bash
hello.exe
# Hello, World!

Anatomy of the program

Let's go line by line.

#include <iostream>

This is a preprocessor directive. It tells the compiler to include the iostream (input/output stream) header from the C++ Standard Library. Without it, std::cout doesn't exist.

C++
#include <iostream>   // I want to use cout and cin

The #include is processed before compilation. Think of it as copy-pasting the header file's contents at that line.

int main()

Every C++ program has exactly one main function. It's the entry point — where execution begins. int means the function returns an integer to the operating system.

C++
int main() {
    // program starts here
}

std::cout

cout stands for "character output". It's defined in the std (standard) namespace. The << operator sends data to the output stream.

C++
std::cout << "Hello, World!" << std::endl;

std::endl flushes the output buffer and moves the cursor to a new line. You can also use ' ' which is slightly faster (it doesn't flush):

C++
std::cout << "Hello, World!
";

return 0;

The main function returns 0 to signal success to the operating system. Any non-zero value means something went wrong. Modern C++ actually makes this optional — the compiler adds it automatically — but it's good practice to be explicit.


What the compiler does

When you run g++ -o hello hello.cpp, four things happen:

  1. Preprocessing#include directives are expanded, macros replaced
  2. Compilation — C++ source is translated to assembly
  3. Assembly — assembly is turned into machine code (an object file .o)
  4. Linking — object files and standard library code are combined into one executable

You can see the intermediate steps:

Bash
g++ -E hello.cpp -o hello.i   # Stop after preprocessing
g++ -S hello.cpp -o hello.s   # Stop after compilation (assembly output)
g++ -c hello.cpp -o hello.o   # Stop after assembly (object file)
g++ hello.o -o hello          # Link

In practice you just run g++ -o hello hello.cpp — one command, all four steps.


Useful compiler flags

Bash
# Strict warnings (recommended from day one)
g++ -Wall -Wextra -o hello hello.cpp

# C++17 standard (use modern features)
g++ -std=c++17 -Wall -Wextra -o hello hello.cpp

# Debug build (includes debug symbols for gdb)
g++ -std=c++17 -g -o hello hello.cpp

# Optimised release build
g++ -std=c++17 -O2 -o hello hello.cpp

Add -std=c++17 -Wall -Wextra to every compile command from now on. The warnings will save you hours.


Common errors beginners hit

Forgot the semicolon

C++
std::cout << "Hello"  // ← missing ;
// error: expected ';' before 'return'

Every statement in C++ ends with a semicolon.

Forgot #include

C++
int main() {
    std::cout << "Hello
";  // error: 'cout' is not a member of 'std'
}

Fix: add #include <iostream> at the top.

Used cout without std::

C++
cout << "Hello
";  // error: 'cout' was not declared in this scope

Either write std::cout or add using namespace std; after your includes (though std:: is generally preferred in real code — it makes it clear where things come from).


Key takeaways

  • C++ compiles to native machine code — that's why it's fast.
  • Every program starts at main().
  • std::cout prints to the terminal; << is the insertion operator.
  • Compile with -std=c++17 -Wall -Wextra from the start.
  • The compiler works in four stages: preprocess → compile → assemble → link.

Next up: variables, data types, and operators — the building blocks of every C++ program.


Was this article helpful?

w

webencher Editorial

Software engineers and technical writers with 10+ years of combined experience in algorithms, systems design, and web development. Every article is reviewed for accuracy, depth, and practical applicability.

More by this author →