Hello World

On the last page we tested our C++ configuration with the below code:

#include <iostream>

int main() {
    std::cout << "Hello world!\n";
    return 0;
}

I’ll admit, this is quite a lot of code needed to just print some words on the screen. In many other languages this can be done by simply typing print("Hello world!"). So here, I’ll walk through how this program works.

#include <iostream>

By default, C++ programs don’t have access to all available C++ features, so you often need to “include” specific files to use certain features in your code. For example, you can add #include <random> to give you the ability to generate random numbers or you can add #include <cmath> to give you access to more math functionality. Similarly, in order to display text on your computer screen you need to add #include <iostream> to the top of your code. This allows you to utilize std::cout, which I describe later in this page.

int main() {

This line is the beginning of a “function” called “main” (I’ll go over functions in more detail in the Functions page of this guide). All you need to know now is that a function is a chunk of code that has a name associated with it (in this case, “main”), and every C++ program must have a function, like this one, called “main”. This tells your computer where to actually begin reading through your code.

Now, you’re probably also wondering what “int” means. “int” stands for integer (any number that doesn’t contain a decimal point) and it refers to the type of data that this function “returns”. I’ll explain function returns when going over the line “return 0;”

Also note that all functions must contain a set of parentheses after their name and must begin with a curly brace. Again, I’ll talk more about this in the “Functions” page.

std::cout << “Hello world!\n”;

This is where the phrase “Hello world!” is actually displayed on your screen. There are quite a few things to take from this single line of code:

  • std::cout comes from the iostream file, which we included at the top of the program. Any text that follows std::cout with two less-than symbols is printed to the screen. Note that lots of C++ code contains “std::” before certain words, which just serves to indicate that the code comes from the “C++ Standard Library” (basically just the set of built-in C++ features). Later on you’ll see that “std::” is considered a C++ “namespace”.
  • The two less than symbols are referred to as the “insertion operator”. In this case, the insertion operator is used to insert text to the “stream” of text to be displayed on your screen.
    • Try it: You can chain insertion operators together, so the following line would print the same text to your screen:
std::cout << "Hello " << "wo" << "rld!\n";
  • “Hello world!\n” is called a “string” because it consists of a series of characters. All strings are surrounded by quotes (to distinguish them from actual code).
    • The “\n” at the end of the string is called the line feed or new line character, which causes any following text to be displayed on a new line.
      • Sometimes you may see “\n” replaced by std::endl as in std::cout << "Hi" << std::endl; . However, both generally work in the same way.
    • Try it: See what happens with the below code:
#include <iostream>

int main() {
    std::cout << "Hello world!\n";
    std::cout << "Hello again.";
    std::cout << "And" << std::endl << "again.";
    return 0;
}
  • Also note that any line of code within a function in C++ must be followed by a semicolon.

return 0;

Like I wrote earlier, the main function in C++ “returns” an integer (0, in this case). Now, what does this mean? Basically, the number that the main function returns can let you or your computer know if anything went wrong in the C++ program. Returning 0 indicates to you and your computer that your program had run successfully and can now exit. You can return any other value to indicate that something went wrong.

Don’t worry if you don’t fully understand function return values just yet. I’ll be going over exactly how they work a little later on.

}

Last line! Here, we just let the C++ compiler know that the main function ends on this line. In other words, this curly brace closes the opening curly brace from line 3.

One last thing: While it isn’t required, it’s always helpful to indent all code that’s within curly braces. This just makes it a lot easier to tell what code is actually inside of them.

Challenge Problem

Write a program that will display a triangle on your screen, like the one below:

  *  
 ***
*****

If you get stuck, check out my solution here.