Difficulty: Beginner
Programming Language: C++

It's finally time to write your first program!

Below is an example of the most basic program that any programmer could write. When this application is executed from a terminal window (such as Command Prompt for Windows or Terminal for Mac and Linux) it simply prints "Hello World!" to the screen, then returns back to terminal. We will first show the code for this example and then follow up with a line by line breakdown of what is happening.

  1. Preprocessor include.

    #include <iostream>

    Before a C++ program is parsed and compiled, a program called the preprocessor is run. Any line starting with a hash # is an instruction for the preprocessor. The #include instruction requests that the preprocessor should locate and parse the file specified before continuing to parse the current file.

    If we specify the filename within quotes #include "FILENAME" the preprocessor will search our project directory for the file. If we specify the filename within angle brackets #include <FILENAME> the preprocessor will search the default environment directories, and additional directories that the compiler is configured to use. When installing a compiler it should include the standard C++ libraries in the compilers installation directory and then these will automatically be added to the compilers configuration.

    Here the file iostream is used to import the C++ standard library that allows us to communicate to the standard input and output streams. For most applications this will be the terminal window that is used launch the application. The standard input stream enables us to retrieve information from the user via the terminal window and the output stream allows us to write text back to the user.

  2. An empty line.

    Empty lines, spaces and tabs are collectively known as whitespace. Whitespace is ignored in C++ unless it is required to separate multiple expressions.

    Due to whitespace being ignored we can use it to our advantage in order to separate sections of our code with a number of empty lines; indent areas of code; and any other kind of formatting that helps to make our code cleaner and easier to read.


  3. The 'main' function definition.

    int main() {

    We won't go into a lot of detail as functions will be covered later, for now it's enough to know that every application needs a main function which is coded as above.

    In C++ the main function provides the entry point for any application, and when the code in the function body has completed executing the application closes.

    int main() is the function signature (this will be explained in functions). The opening curly brace { denotes the start of our function body and the closing curly brace } marks the end. Anything between the matching pair of curly braces is the body of the main function.


  4. Output to the terminal.

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

    Once again we'll cover more detail later, for now we'll take a high level view of what this line of code is doing.

    std::cout is part of the iostream (notice the file we included earlier) and represents the console output stream, otherwise known as the standard output stream.

    << is used to pass the next item to the stream, causing it to be printed out to the console. We pass text to the output steam by surrounding it with quotes string literal to the stream with the value of "Hello World!\n".

     

    We use "\n" as a special character to represent that a new line should be created. Different operating systems use different character codes to indicate when a new line should occur, but std::cout will take the '\n' character and translate it to the correct newline character for the operating system that you compile on. There are several of these special character combinations known as escape characters, which we'll cover in a later article.

    Finally we have the semicolon ; which represents the end of our statement. Since C++ ignores white-spaces we are able to start a statement (an instruction to the computer) on one line and finish it on another, so we have to use something to complete the statement. C++ and many other "C-like" languages use the semicolon to denote the end of a statement.


  5. Return code.

    return 0; 

    The return statement passes a value back to the function caller. In the case of an applications main function the caller is usually the operating system the application is running on. The value of 0 is conventionally used to indicate that the application executed successfully with no errors, while any non-zero value are used to indicate the reason why the application did not exit successfully. The values used for error return codes are arbitrary and may be defined for each application, however your host operating system may have a number of standard error codes that you can take advantage of instead of creating your own list of error codes.