Modern C++: The Good Parts/Anatomy of a global greeting

Let's examine our program from last time in more detail.

Code & explanationEdit

#include <iostream>
#include <string>

These includes are required by the standard, so anyone who wants to offer a proper C++ compiler must provide them. Several includes are required that way, and the set of them is called the standard library.

  • <iostream> allows us to use std::cout and std::cin. All of our programs will include it, because they're all console programs.
  • <string> is needed anytime you want a string (a value that represents text), which will also be all of our programs.

int main()
	std::cout << "Hello, world!\n";

As mentioned before, int main is required for all C++ programs. Ignore the parentheses and curly brackets for now; just know that they're necessary.

std::cout and std::cin are for console output and input, respectively. std is the namespace, which basically means who gave them to us, and this one is the standard namespace - i.e., these are from the standard library. The "c" just means C++ inherited them from its "parent" language, C.

The "\n" at the end of the string is a line break or newline; it causes further text to be printed on an additional line.


standard library
several useful pieces of code required by the C++ standard, shipped with all C++ compilers, all under the namespace std.
a value that represents text.
a directory of names, or where to look for something by name.
a character which separates two lines of text. Also called a line break. Syntax: \n
Modern C++: The Good Parts
 ← Hello, world! Anatomy of a global greeting The world's response →