Beginner

Hello World in C++

Your first C++ program - the classic Hello World example with Docker setup

Every C++ journey begins with Hello World. While similar to C’s version, the C++ implementation showcases the language’s more expressive syntax through the iostream library and namespaces.

The Code

Create a file named hello.cpp:

1
2
3
4
5
6
#include <iostream>

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

Understanding the Code

  • #include <iostream> - Includes the input/output stream library, providing std::cout and std::endl.
  • int main() - The entry point of every C++ program. Returns an integer to the operating system.
  • std::cout - Character output stream from the standard namespace. The << operator sends data to the stream.
  • std::endl - End line manipulator that outputs a newline and flushes the stream.
  • return 0 - Returns 0 to indicate successful execution (this can be omitted in main() as C++ automatically returns 0).

Running with Docker

The easiest way to run this without installing a C++ compiler locally:

1
2
3
4
5
6
# Pull the official GCC image (includes g++)
docker pull gcc:14

# Compile and run the program
docker run --rm -v $(pwd):/app -w /app gcc:14 \
    sh -c "g++ -o hello hello.cpp && ./hello"

Running Locally

If you have g++ or clang++ installed:

1
2
3
4
5
6
7
8
# Compile with g++
g++ -o hello hello.cpp

# Or compile with clang++
clang++ -o hello hello.cpp

# Run the program
./hello

With Modern C++ Standards

1
2
3
4
5
6
7
8
# Compile with C++17
g++ -std=c++17 -o hello hello.cpp

# Compile with C++20
g++ -std=c++20 -o hello hello.cpp

# Compile with C++23
g++ -std=c++23 -o hello hello.cpp

On Windows with MinGW or MSVC:

1
2
3
4
5
6
7
# MinGW
g++ -o hello.exe hello.cpp
hello.exe

# MSVC
cl /EHsc hello.cpp
hello.exe

Expected Output

Hello, World!

Key Concepts

  1. C++ is compiled - Source code (.cpp) is compiled to machine code, just like C
  2. iostream vs stdio.h - C++ provides type-safe I/O through streams instead of C’s printf
  3. Namespaces - std:: prefix indicates the standard namespace, preventing name conflicts
  4. Operator overloading - The << operator is overloaded to work with streams
  5. Implicit return - In C++, main() implicitly returns 0 if no return statement is present

Alternative Approaches

Using namespace directive

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

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

While this removes the need for std:: prefixes, it’s generally discouraged in larger programs to avoid namespace pollution.

Using printf (C-style)

C++ is backward compatible with C, so this works too:

1
2
3
4
5
6
#include <cstdio>

int main() {
    std::printf("Hello, World!\n");
    return 0;
}

However, the iostream approach is preferred in C++ for type safety and extensibility.

Modern C++20 with std::format

1
2
3
4
5
6
7
#include <iostream>
#include <format>

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

C vs C++ Comparison

The equivalent C code would be:

1
2
3
4
5
6
#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

The C++ version offers:

  • Type-safe I/O through streams
  • No format string vulnerabilities
  • Easier to extend with custom types
  • Object-oriented stream manipulation

Next Steps

Continue to Variables and Data Types to learn about C++’s rich type system, including modern features like auto and type inference.

Running Today

All examples can be run using Docker:

docker pull gcc:14
Last updated: