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.


  2. An empty line.

    Empty lines, spaces and tabs are collectively known as whitespaces. Whitespaces are ignored in C++ everywhere aside from separating mutliple expressions.

    Due to whitespaces being ignored we can use them to our advantage to separate out sections of our code with a number of empty lines, indent our code to help us identify different nested levels easier, and anything else that makes 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!" << std::endl;

    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 con sole 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!".

    We then use << std::endl to send the value of std::endl to the stream. This value is used to represent end of line which indicates to the terminal that the cursor position should move to the next line. The std::endl token is used because different operating systems use different characters to represent a new line, your compiler will use the correct character for the operating system that you compile your code on.

    Finally we have the semicolon ; which represents the end of our statement. Since C++ ignores whitespaces 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 instruction. C++ and many other "C-like" languages use the semicolon to terminate 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, any non-zero value is 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.