C++ Tutorial 1 - Hello, World!

Welcome to the first tutorial. Our starting point will be a fairly brief primer on a modern programming language that will allow us to implement and use some of the more advance algorithms and data structures later on. If you already familiar with C++ or are confident that you can understand the material without understanding our language of choice, if for example you are knowledgeable in Java or C#, then this tutorial series is probably not necessary.

Before we begin writing code let say what this tutorial is not. This is not a lesson about how to use your computer or how to set up your software. I will not answer questions about how to fix Xcode or Visual Studio or any other IDE or tool chain. I cannot know exactly what operation system, or hardware, or particular version of software you are using and so I cannot be expected to solve problems as they arise. There are probably good how-to's and guides for whatever your chosen environment is. Because I know that there will be questions I will answer in advance what I recommend using. If you are using Windows I recommend using Visual Studio, most of its features are free and it will fit most of your needs, Code::Blocks is a reasonable alternative but it is less standard on Windows so I recommend the dominant environment, Microsoft's Visual Studio, unless you need compatibility with something like GCC. If you are using OS X you're probably more familiar with the software than I am. Xcode is the main IDE, integrated development environment, but I'm not certain which compilers are available on that platform. If you are using Linux, congratulations for being in the 1% of computer users, probably your best choice is to use a simple editor, my choice is Atom but there are many others, and one of the two popular compilers for that platform, Clang or GCC, and compile your code via the terminal. My personal choice is for the latter but it might be worth your time to experiment and chose what works best for you after you gain a little experience. Now onto the coding.

In order to go about explaining the inner workings of computer code it is probably best to simply jump into a program and explain how it works line by line. Below is the classic Hello, World! program which almost every CS student is exposed to at first.

#include <iostream>
using namespace std;

int main()
cout << "Hello, World!" << endl;
return 0;

Going line by line from the top the first two lines of code refer to something called the standard library. This library is standard to all C++ compilers, but there are several different versions of the standard library which have evolved over time, c++98, std0x, c++17, etc, all of these versions are backward compatible however so there should not be any issue with your compiler running this code. If there are compilation issues and there are not due to input error check the manual or message boards for your compiler. Later on we will include other files and namespaces in our projects but for now it is not necessary for you to understand exactly what those two lines of code mean.

You may notice that the line of code directly after those two is an empty line of code, more commonly called whitespace, which we include only to make the code more readable. It is in no way required, because C++ is not a whitespace sensitive language, but it is strongly recommended to include as many white spaces as you need to make the code easier to read for yourself and for others. For large projects it is common for there to be standards on how to properly indent code; this tutorial will be using an indent style similar to Allman style.

After the whitespace is declaration of the main function. The main function is found in every program written in C++. The purpose of this function is for the compiler, and the programmer, to determine the start of code execution. The type of our main function is int, which stands for integer, and while there are other acceptable types for the main function it is considered standard to make the main function an int. We will discuss other types in a later in the next tutorial.

After the declaration of the main function there is single character inhabiting the next line of code, an open brace. This open brace is not strictly necessary, but if you do not include it you will have to indent the two following lines of code and also delete the close brace. This line does not in any way affect the execution of the program but it does cordon off certain sections of code and it is required for some code. We could have also added an additional set of braces around the two already in the program. This would be written like {{ and }}.

Finally we are inside of the main function and the start of our program. This first line of code actually does two things. The first thing that it does is print the phrase, string, Hello, World! to the standard output. The next thing that it does is prints a new line character to the standard output. This character the computer to start a new line. An example of this newline character is the whitespace character example above. You may notice what probably appears to be a double less than, <<, preceding each line which the computer prints. They also have have a corresponding double greater than, >>, but they both are in fact bitshift operators. We will revisit the actual purpose of these operators in much later tutorial but for now it is fine to think of them as input and output operators. Before we go to the next line of code you should also notice that the this line of code ends in a semicolon, ;, which is required to compile. These semicolon act in the same way that period, ., acts in a human language. The second line of code also ended in a semicolon but the first line of code did not. Not every line of code in C++ has to end in a semicolon, but there are is a system to which lines must have a semicolon and which ones must not. In particular the statements which may not end that way are control statements, more on those in the third tutorial.

The next line of code is a return statement. This line does exactly nothing for the execution of this program and it is not necessary, however there are circumstances where it is necessary for a function to return a particular value. For example if you were to write a function which took the average of a set of numbers you would have to return that average after computing it.

That's the end of our first program. It doesn't do very much but it does introduce some of the basic concepts to the reader for the next program. Each one of these tutorials builds off of the previous and without understanding most of the material in a given tutorial the next one will become that much more difficult.