C++ Getting started with C++ Hello World


This program prints Hello World! to the standard output stream:

#include <iostream>

int main()
    std::cout << "Hello World!" << std::endl;

See it live on Coliru.


Let's examine each part of this code in detail:

  • #include <iostream> is a preprocessor directive that includes the content of the standard C++ header file iostream.

    iostream is a standard library header file that contains definitions of the standard input and output streams. These definitions are included in the std namespace, explained below.

    The standard input/output (I/O) streams provide ways for programs to get input from and output to an external system -- usually the terminal.

  • int main() { ... } defines a new function named main. By convention, the main function is called upon execution of the program. There must be only one main function in a C++ program, and it must always return a number of the int type.

    Here, the int is what is called the function's return type. The value returned by the main function is an exit code.

    By convention, a program exit code of 0 or EXIT_SUCCESS is interpreted as success by a system that executes the program. Any other return code is associated with an error.

    If no return statement is present, the main function (and thus, the program itself) returns 0 by default. In this example, we don't need to explicitly write return 0;.

    All other functions, except those that return the void type, must explicitly return a value according to their return type, or else must not return at all.

  • std::cout << "Hello World!" << std::endl; prints "Hello World!" to the standard output stream:

    • std is a namespace, and :: is the scope resolution operator that allows look-ups for objects by name within a namespace.

      There are many namespaces. Here, we use :: to show we want to use cout from the std namespace. For more information refer to Scope Resolution Operator - Microsoft Documentation.

    • std::cout is the standard output stream object, defined in iostream, and it prints to the standard output (stdout).

    • << is, in this context, the stream insertion operator, so called because it inserts an object into the stream object.

      The standard library defines the << operator to perform data insertion for certain data types into output streams. stream << content inserts content into the stream and returns the same, but updated stream. This allows stream insertions to be chained: std::cout << "Foo" << " Bar"; prints "FooBar" to the console.

    • "Hello World!" is a character string literal, or a "text literal." The stream insertion operator for character string literals is defined in file iostream.

    • std::endl is a special I/O stream manipulator object, also defined in file iostream. Inserting a manipulator into a stream changes the state of the stream.

      The stream manipulator std::endl does two things: first it inserts the end-of-line character and then it flushes the stream buffer to force the text to show up on the console. This ensures that the data inserted into the stream actually appear on your console. (Stream data is usually stored in a buffer and then "flushed" in batches unless you force a flush immediately.)

      An alternate method that avoids the flush is:

      std::cout << "Hello World!\n";

      where \n is the character escape sequence for the newline character.

    • The semicolon (;) notifies the compiler that a statement has ended. All C++ statements and class definitions require an ending/terminating semicolon.