This file contains the classic minimal example of a C++ source file, “Hello World”. First, the file specifies that the iostream standard library is used here and it should be included during compilation. Next, it imports the names of functions and constants from the std namespace. Specifically, cout and endl are imported to make the code more readable (and generally faster to write) than if we had to specify std::cout and std::endl.

The core of this file is the main function. Preceding the word main is the specification that this function returns an integer, int. Within the input parentheses is the word void, which indicates that this function takes no input. All main functions in this course having the same prototype: int main(void). The curly braces, {}, contain the contents of the main function.

The first line of the function outputs two things: the string "Hello, World!" and the carriage return endl. This character is equivalent to pressing the Enter key to go down to the next line. The second line of the function returns the integer 0. This is the expected behavior of a main function, indicating that the preceding code executed successfully.

// 1. Filename: hello_world.cpp

// 2. Preprocessor directives
#include <iostream>

// 3. Namespace declarations
using namespace std;

// 4. Program entry point
int main(void){

    // 5. Code statements
    cout << "Hello, World!" << endl;

    // 6. Return statement
    return 0;
}

// 7. Comments
// single line comments

/*
multi
line
comments
*/

This file can be compiled by running the terminal command:

g++ hello_world.cpp

This will create an executable named a.exe on Windows computers, and a.out on Mac and Linux. To run the executable, type ./a.exe on Windows or ./a.out on Mac and Linux. The output from running the executable is:

Hello, World!