How to write your first C++ program

In this tutorial, you will learn how to write your first C++ program. It’s pretty simple. All you have to do is to write a “Hello, world!” program that is you have to print Hello, World! on your console screen. Let’s see how you can do this.

First C++ program: “Hello, World!”

Here is a program to print Hello, World! on your screen. First, have a good look at the program and then we will discuss it thoroughly.

The “Hello, World!” program:

//Hello, World program
#include <iostream>
using namespace std;

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


Hello, World!

Understanding the  Hello World Program

Let’s understand the program step by step.

1.//Hello, world program: This is a comment. Comments are not processed by the compiler. In C++, we use // to specify a single-line comment. To know more about comments: How to add comments in C++

2.#include<iostream>: There are many header files in C++ with numerous predefined functions and variables. An iostream header file is one of these. This has many functions defined in it including standard input/output streams that we have used in the program to print the desired output.

3.using namespace std: A namespace is a declarative region with functions and variables and their scopes are limited to that particular region. std is a namespace. In the above program the statement using namespace std; is used to specify that the programmer is going to access that region. To know more on namespaces: Namespaces in C++ 

4. int main(){}: main() function is the gateway to enter into the programmer’s code and process it. It is the main function of our program. The execution of our program begins with this function. The int here is used to tell the compiler that the main() function returns an integer. To know more, see this: main() function in C++

5.cout<<“Hello, World!”: This statement prints “Hello, World!” on the console. cout is an instance of ostream class which belongs to iostream header file. (<<) is the insertion operator used with cout. We have used double quotes because Hello, World! is a string. To know more on this, see this: cout and cin in C++

6.The return statement: At the end of the program we have used return 0; statement. This indicates the ends of the execution. If the program returns 0, that means the program is successfully executed. Otherwise, it means that the execution is failed.


While it is not mandatory to use proper indentation in C++, a good programmer must always make proper use of indentation. As you can see, in the above program, the return statement starts at the same distance from the left as the cout statement. This makes your code more readable.

Thank you.

Leave a Reply