CommonLounge Archive

Your First C++ Program - Hello World

September 11, 2018

First let us talk about why should we learn C++. C++ ranks 2nd in popularity according to 2018 IEEE spectrum Top Programming Language ranking (link). Learning C++ is a wise investment for all programmers.

C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Stroustrup in 1979. C++ is the preferred language of choice for applications where it’s important to write highly-efficient code. These include modern games, operating systems, browsers, and much more.

C++ is also a great programming language as your first programming language since coding in C++ gives you a good idea about how computer memory works, how information is stored in computer memory, and so on.

Let’s get started! In this tutorial, you’ll see the C++ “Hello World” program, and then understand what each line of the code is doing.

The Hello World program

The “Hello World” program simply outputs the words “Hello World”.

// Program to display "Hello World"
#include <iostream> // Header file for input output functions
using namespace std;
// main function - this is where the execution of program begins
int main() 
{
    cout << "Hello World" << endl; // print "Hello World" (and go to new line)
    return 0;
}

Output:

Hello World

Understanding Hello World

Don’t worry if the earlier program is overwhelming. We’ll go through and break down every line of the program below at a high level. We’ll also cover each of these concepts in greater detail later on in the course.

  • //: In C++, // denotes a comment. Comments are used to make the code readable and easy to understand. In our case, the first comment describes what our program does. When the C++ compiler sees //, it ignores everything on that line after the //.
  • #include: C++ has lots of libraries. #include <iostream> tells C++ to include library named iostream (stands for input output stream). Libraries contain code written previously. This library provides functions for input / output. In our program, we will use the cout command later to output some text. Since this command is part of the iostream library, we need to include the library before using the cout command.
  • int main(): This line is used to declare a function named main. The execution of every C++ program begins from the main function, no matter where the function is located in the program. Moreover, the main function is required to return an integer data type. Hence, it begins as int main, and ends with return 0. This is just convention. When we learn about functions later, we will understand what returning something from a function actually does. For now, think of the main function as simply the place where program execution begins.
  • { and }: The opening and closing braces indicate the beginning and end of the function body. (In this case, the body of the main function). Everything between these two braces is the body of the main function.
  • cout << "Hello World" << endl; line tells the compiler to display the message “Hello World” on the screen.
  • cout stands for console output. It says, output something to the console.
  • We use the << operator to tell what needs to be outputted. Note that the << operator can be chained. So if we want to output 3 things, we can do << thing1 << thing2 << thing3.
  • In our program, we give the string Hello World to be outputted. Strings must be enclosed in double quotations ("). Hence, in the program, the string is written as "Hello World".
  • This is followed by endl (for endline). This means that if we use cout again, the values will be printed on the next line, instead of the same line.
  • The cout line is a statement in C++. Every statement is meant to perform some task. Each statement must end with a semicolon (;). If you forget to add semicolon at the end of a statement, the compiler will throw an error when you try to compile the C++ program.
  • cout is part of std namespace. We’ll learn about namespaces in later lessons. This is the reason why we need to have the statement using namespace std; at the top of the file.
  • return 0; is also a statement (hence it ends with a semicolon). This return statement is used in functions to return the results of the tasks performed by a function.
  • The value returned by the main function is used to determine if everything went smoothly. In this case, we return 0 because that is the value conventionally used to denote “Everything went as expected”.
  • Once the return statement is encountered, the function stops executing. That means that if we write more statements inside the main function but after the return statement, those statements won’t get executed.
  • Indentation: The main purpose of indentation in C++ is to make the code easy to read for yourself and others. In this program, we indent the part of the code that is inside the main function (lines 9-11). Note that the C++ compiler doesn’t care about indentation, the code would work just fine without any indentation. However, it is an extremely bad practice to write code without indentation.

C++ “boilerplate” code

You may have noticed from the previous section that there are a lot of code lines that don’t actually produce any end result or have any effect that is visible. The only thing that does some action is the cout << "Hello World" << endl; statement. The rest of the code doesn’t do much, it just needs to be there. We call this boilerplate code. Every time you write code in C++, the program will follow this overall layout.

// This is the "boilerplate" code for C++. 
// All the C++ code you write in this course will have this layout.
#include <iostream> // We will always need the iostream library for input-output
// More #include statements here for adding any other libraries we need 
using namespace std; // We will always need namespace std since cout is inside the std namespace
// main function - where the execution of program begins
int main()
{
    // actual code here 
    // ... 
    return 0; // main always end with "return 0;"
}

From the next tutorial onwards, the code snippets we’ll write will only include the part of the code indicated as // actual code here. When you run the code, it will automatically expand to its full version.

If you are running C++ directly on your computer, make sure you type the provided code snippets inside the body of your main function. We strongly recommend that you write the full code and not copy-paste, as it is good for you to practice writing code.

Installation and Setup

If you don’t want to install things right away and want to get started with learning C++, then you can skip installation for now. You will see a Try it now! button on code snippets in this course, which allows you to run the code directly from your browser / mobile app. You will also be able to edit the code and play around (in-fact, the coding exercises will require you to). In addition, there are also a number of other websites where you can submit and run your code — here are some options: Onlinegdb, Ideone, Jdoodle.

That said, if working online is slowing you down or you prefer to have C++ installed on your computer, then follow the instructions here: Install C++ : Instructions for Windows, Mac and Linux. If you are on Linux (or on Mac OS and don’t want to install Xcode), then read Introduction to the Command-line Interface tutorial to become familiar with the command line before you start the installation process. For beginners, we recommend installing C++ once you are about halfway through the course.

Conclusion

Congratulations!!! You have understood your first C++ program.

If there are some things you feel that you do not understand completely (such as functions), don’t worry. That is expected, and you will learn these concepts in more detail as you go through this course.


© 2016-2022. All rights reserved.