Hello, World! in C++: A Short History and Your First Program
C++ powers game engines, web browsers, databases, trading systems and much of the software where speed matters most. It builds on C, so if you’ve seen C code, a lot of it will look familiar. Here’s where C++ came from, and how to write and run your first program.
A short history of C++
In 1979, Danish computer scientist Bjarne Stroustrup started work at Bell Labs, the same place C had been created a few years earlier. For his PhD he had used a language called Simula, which organized programs into classes and objects. He liked that way of structuring large programs but found Simula too slow, so he set out to add classes to the fast, practical C language. He called the result “C with Classes.”
In 1983 it was renamed C++. In C, ++ is the operator that adds one to a number, so the name means “one more than C,” or “the next step after C.” The first commercial version was released in 1985, along with Stroustrup’s book The C++ Programming Language.
C++ became an international standard in 1998 (C++98). After a long gap came C++11, a major update that modernized the language so much that people speak of “modern C++.” Since then a new standard has arrived every three years: C++14, C++17, C++20 and C++23.
C++ is known for giving programmers a great deal of control over how their programs use memory and the processor, while still offering high-level features. That combination is why it’s still a first choice for software that has to be fast.
The code
Save this in a file called hello.cpp:
#include <iostream>
int main() {
std::cout << "Hello, World!\n";
return 0;
}
What each part does:
#include <iostream>: brings in the input/output stream library, which providesstd::cout.int main(): the main function, where every C++ program starts. It returns a whole number (int) to the operating system.std::cout << "Hello, World!\n";: sends the text to standard output (the terminal).std::meanscoutcomes from the standard library, and<<means “send this to the output.”\nstarts a new line.return 0;: tells the operating system the program finished successfully.
You’ll often see std::endl used instead of \n. Both end the line; std::endl also forces the output to be written immediately, which is a little slower and usually unnecessary.
Steps to run it
Like C, C++ is compiled: a compiler turns your code into an executable file, which you then run.
1. Install a compiler
- Mac: open Terminal and run
xcode-select --installto get Apple’s command line tools, including the Clang C++ compiler (clang++, also available asg++). - Linux: install the GNU C++ compiler. On Ubuntu or Debian:
sudo apt install build-essential. On Fedora:sudo dnf install gcc-c++. - Windows: use MSYS2 (msys2.org) to install
g++, or Visual Studio with the “Desktop development with C++” workload. You can also use WSL and follow the Linux steps.
Check it worked:
g++ --version
2. Compile
In a terminal, go to the folder containing hello.cpp and compile it:
cd path/to/your/folder
g++ hello.cpp -o hello
This creates a file called hello (hello.exe on Windows). No output from the compiler means no errors.
3. Run
./hello
On Windows, run .\hello.exe in PowerShell, or hello in Command Prompt. The terminal prints:
Hello, World!
A newer way to print
C++23 added std::println, which works more like printing in Python or Java and adds the newline for you:
#include <print>
int main() {
std::println("Hello, World!");
}
It needs a recent compiler; with GCC, compile it with g++ -std=c++23 hello.cpp -o hello. If your compiler doesn’t support it yet, stick with std::cout, which works everywhere. (Leaving out return 0; is allowed in main, which returns 0 automatically.)