Hello World in C++
The Hello World program is introduced to new programmers as its the simplest program to write in many languages (except GUIs). The Hello World program we’ll be writing in C++ will display “Hello World!” to the terminal.
By now you must have your C++ programming environment set up, if not check out C++ Installation.
Writing your first program
The easiest way to learn how to program is by demonstrating a simple example. Simply compile the following code.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { // Will display Hello World to the terminal cout << "Hello World!"; return 0; } |
The basic structure of a C++ program
So if you were successful, you should have Hello World displayed in your terminal. Below, I’ll go ahead and describe the basic structure of a C++ program.
The following tells the compiler to include the following library since we’ll be using some of its functionality in our program.
#include <iostream>We’ll save namespaces for a later tutorial as its a more advanced feature. For now, just know that functionality found in iostream is declared under a namespace std. This will be something that will be found in most of your programs.
using namespace std;
The following is a functional declaration which we’ll describe later. The purpose of the main function is that it points to where your C++ program will start executing always. The {}’s simply encapsulate the body of our program.
int main()
The following are known as comments. They are useful as notes to the programmer and others who wish to edit the code later on. Their purpose is to describe the functionality of a certain block of code. Best practice is to have clear and concise comments to let the reader know what to expect.
// Will display Hello World to the terminalThe following is known as a C++ statement as it terminated with a semicolon (;). The following cout statement belongs to the
cout << "Hello World!";
As mentioned before, our main function which expects an int value in return. The 0 denotes that the program terminated successfully.
return 0;
Overview of C++ program structure
Described above is the most basic of C++ programs. For the next couple of tutorials, we’ll be using the following format. Make sure you understand the basic gist as its imperative you understand the structure of a C++ program.

Pingback: c++ – Latest c++ news – C++ Hello World | Talk Binary