Hello World in C++

Our first program will print the classic “hello world” message. Here’s the full source code.

#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}

To run the program, save the code in a file named hello-world.cpp and compile it using a C++ compiler like g++.

$ g++ hello-world.cpp -o hello-world
$ ./hello-world
hello world

Sometimes we’ll want to build our programs into binaries. In C++, compiling the code as shown above already produces a binary that can be executed directly.

$ ls
hello-world  hello-world.cpp

We can then execute the built binary directly.

$ ./hello-world
hello world

Now that we can run and build basic C++ programs, let’s learn more about the language.