Our first program will print the classic “hello world” message. Here’s how to do it in different C++ versions.

// This basic version works in C++11 through C++20
#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}
// Same as C++11
#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}
// Same as C++11
#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}
// Same as C++11
#include <iostream>

int main() {
    std::cout << "hello world" << std::endl;
    return 0;
}
// C++23 introduces a simpler way using std::print
#include <print>

int main() {
    std::print("hello world\n");
    return 0;
}

To run the program, save it as hello-world.cpp and compile it:

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

If you want to use C++23’s std::print:

$ g++ -std=c++23 hello-world.cpp -o hello-world
$ ./hello-world
hello world

Let’s break down the basic code:

  1. #include <iostream> - Includes the input/output library
  2. int main() - The starting point of the program
  3. std::cout << "hello world" << std::endl; - Prints “hello world” and adds a new line
  4. return 0; - Ends the program successfully

The code is identical from C++11 through C++20. C++23 introduces std::print which provides a simpler way to print output.

Next example: Values