Blog Tech C++ Hello World: An Introduction to Programming in C++

C++ Hello World: An Introduction to Programming in C++

We’re surrounded by programs. Take a household coffee machine, for example. It’ll have a program that calculates the optimal amount of water and temperature needed to make you a cup of coffee. Inside the coffee machine, there’s a small computer running automated instructions — written by a programmer, using a language like C++. 

What is programming about, and how does it look in practice? In this article, we’ll go over the basics of coding in C++, and you’ll even get to create your first program! Let’s get started.

So What Is Programming?

A “program” is commonly defined as “a plan or schedule of activities, and procedures to be followed.” When you plan out your workday or next holiday, you’re likely defining a structure of consecutive actions that you’ll undertake. These actions may be subject to conditions, such as, “If the restaurant is fully booked, we’ll eat a hamburger at the beach instead.” The process of writing a program is quite similar.

It’s All About Automation

When we talk about programming in the computing world, we refer to our use of machines to automate actions that are otherwise repetitive and/or too complex for us to perform. Let’s say you need to calculate your daily travel spend. Rather than doing the math manually on a napkin, you’re better off relying on a mobile app to do that for you. To calculate your gas expenses, you simply input the number of miles driven! Not only will the program do the math correctly, but it can easily calculate the price of fuel based on where you’re located.

How Did Programming Begin?

Back during World War II, Nazi Germany had been using a machine called Enigma to encrypt their military communications. Although their encryption method was basic compared to today’s standards, decrypting an intercepted message was a very time-consuming task for the Allies.

By the time the encryption was broken, the messages were obsolete. The Allies’ process was done by hand, and the Nazis made encryption even harder by  modifying their encryption key every 24 hours. The Allies couldn’t keep up, they required a machine that could perform the decryption at a much greater speed.

British mathematician Alan Turing solved the Allies’ dilemma by creating an electro-mechanical device that could perform different sets of instructions by reading paper programs. The Allies called this device “Bombe,” and that’s how the first computer was born. Bombe helped the Allies decrypt their enemies’ messages quickly enough to take action and counter their plans. Modern computers are still based on the same principles elaborated by Alan Turing in his Turing machine models. (Turing, in turn, was inspired by Charles Babbage’s 1837 Analytical Engine and Ada Lovelace’s subsequent understanding of it as a “universal engine.”)

Why “Hello World”?

You’ve possibly come across the phrase “Hello World” in the programming context. In fact, “Hello World” is often the default program when you first open a code editor.

The phrase comes from the first example in the 1978 book “The C Programming Language,” by Brian Kernighan and Dennis Ritchie. Many early programmers used this book to learn the C language, which is C++’s predecessor. “Hello World” caught on as a tradition—you’ll quite often see the phrase when starting to learn a programming language, or testing your first program. We’ll extend that tradition below. 

Your First Program in C++

We’ll now walk you through creating your first C++ program. You’ll only need to install a development environment and some basic tools. Follow along! 

Prerequisite: Install an IDE

Before writing and running your first C++ program, you’ll need to install an IDE (Integrated Development Environment). This software allows you to write code and turn it into instructions that your computer will readily understand. Depending on your operating system, we recommend the following IDEs for your first C++ project:

How To Make a Computer Say Hello

To begin, open Visual Studio, Xcode, or Code::Blocks and start a new project. Select “New console project,” give it a name, and let the program prepare your first bit of code.

Here’s how this looks on Xcode (Mac), Visual Studio (Windows) and Code::Blocks (Linux):


Visual Studio


Click “Next” and “Finish.” The IDE will now create a sample program that you’ll be able to modify by opening the file “main.cpp”, which is where you write code.

What you see now is a text editor — it may already contain code. Delete any contents and copy/paste this code instead:

#include <iostream>

int main()
std::cout << "Hello World! Welcome to your first C++ program!" << std::endl;

This code contains all of the information needed to run your first program. Executing it will display a message in the console. Let’s look at the code. The first line contains:

#include <iostream>

iostream is a C++ library that contains functions such as the one that’s used to display a message on-screen. A function is a block of code that performs a task. C++ has many libraries available, and you’ll include those you’ll need at the beginning of your program, one per line. If we remove this library, the program won’t run. Let’s analyze the program itself, which starts with: 

int main()

When you run a program, it begins by calling a special function. This function must be called main, as that marks the program’s starting point.

The int indicates the type of message that the program will return once it finishes running. The main() function returns an integer. Usually, it returns 0 when there are no errors. This would indicate to another program that everything went smoothly for this function.

The opening curly bracket { indicates the beginning of the main() function. This is part of C++ standard syntax and where we write code.

The following code is the syntax used to display a message:

std::cout << "Hello World! Welcome to your first C++ program!"; 

std::cout is the standard output stream and the text between the two quotation marks ” is going to be displayed to the user when the program is run. You use quotation marks to tell the compiler that the content between them should not be interpreted as code — it’s just text. The semicolon at the end of the line is the statement terminator. It marks the endpoint of the instruction in question.

The closing curly bracket marks the end of the main() function and the end of the program.

If you hit the “run” button (the arrow at the top or top left of your screen), the program will execute and you will see its output in the console window.

In Visual Studio:

In Xcode:

In Code::Blocks

The compiler interpreted the code, turned it into a lower level code that the computer could understand, and then the computer executed it. The result is the sentence “Hello World Welcome to your first C++ program!” printed in your command prompt.

Learn Programming With Udacity

Congratulations, you’ve just run your first program! C++ is the programming language of choice for thousands of programmers around the world, so welcome to the club.

To continue your coding journey, Udacity offers a variety of nanodegree programs based on your coding experience. 

If you’re completely new to coding, check out our Introduction to Programming nanodegree, where you’ll learn programming fundamentals through HTML, CSS, and Python.

If you have intermediate knowledge of any language and want to learn C++, head over to our Become a C++ Developer nanodegree, where you’ll code five real-world projects.  

Stay updated on the latest in technology

"*" indicates required fields

Stay updated on the Latest in Tech

Be first to hear about special offers, news, and resources from Udacity

"*" indicates required fields