Project Home

C++ Programming Tutorial

Section 2: First Program: Hello World

It's customary in the world of computer science that whenever you learn a new programming language, you always begin by writing a program to display the text "Hello World" to the screen. In programming, displaying text is referred to as "printing," so we're going to learn how to print "Hello World".

Whenever we begin a C++ program, we begin by writing a function called "main". A function is a group of instructions about how to do some common task. The main function is the first function that runs when we run our C++ program, and it often contains other functions inside it. Using the cereal example above, imagine that we had a function called "getMilk" that contained instructions for how to get and open the milk from the fridge. Let's say we had functions for each part of the algorithm above. In our main function, we would "call", or ask the computer to run, each of those functions in the order that we wanted them to occur.

Sometimes, functions are already written for us and are stored in libraries. Using these functions saves us time and makes programming easier. For example, in C++ , you could write lines and lines of code to make the computer print a message to the screen. Or, you could use the iostream library that already has a function called cout, which is much simpler! (Don't worry, we'll just learn how to use cout, and not worry about the iostream library.)

Ok, we're ready to write our first program! Following the instructions for compiling and running via the command line (we suggest you open this page in a new tab and leave it open for reference), copy the following program, save it, and run it. Then read on for a description of what each part of the program does. Note that the first two lines are very important, so you should copy them, too!

#include <iostream>
using namespace std;

int main(){
     cout<<"Hello World!\n";

Now we'll take a look at the code line-by-line. First, we tell the computer to include all the functions included in the iostream library when it compiles the program, so that we get to use cout. We always enclose library names in angle brackets. We then tell it to use all the standard functions included in std with the command using namespace std;.

Next, we define our main function by saying int main(). This tells the computer that there is a function called main that returns an integer (more on this later). We then put an open brace to begin the code that goes inside the main function.

We only have one line of commands for this program. We tell the computer to use the cout function and then put what we want to display after <<. You can think of the double-left angle brackets as an arrow pointing toward cout, meaning that we are sending something (in this case, "Hello World\n ") to cout. We put quotation marks around the words we want to display (this is called a string), and end the string with "\n". This is a special character that tells the computer to go to the next line, kind of like hitting enter at the end of a line when you are typing. In C++, every code line (ones without braces) ends in a semi-colon. So, we are telling the computer to send our string to cout to be displayed on the screen. We then use a closing brace to end the function, and with it, the program.

Task 2.1:  

Can you modify the Hello World program so that it prints, "Hello World and Your Name"? For example, if your name is Justine, it would display, "Hello World and Justine". Can you add a second line of text?

Yay! You have written your first C++ program. Now, you'll learn how to perform simple math.