Hello World Example in C++

In this C++ program we will print a message “Hello World”.

using namespace std;
int main()
    //This will print the message Hello World
    cout<<"Hello World";
    return 0;


Hello World


  • The first line in the program is written to include the iostream header file in our program. This is required because we need the cout object which is an object of iostream class residing in iostream. Failing to do this, would generate an error. Note :- The concept of classes and objects would be clear once we start learning the object oriented programming part.
  • The second line specifies that we are using the namespace std (abbreviated for standard). After that we can use all the entities within the std namespace. If we don’t want to use this line, we have to write the cout object like this-  std::cout We will learn about namespaces in a separate chapter.
  • The third line is the main function. Every C++ programs starts execution with the main function. The curly braces { and } represents the starting and ending of the function. the main function is declared as ‘int main’, where int is the return type of the main function. This means this function will return an integer value.
  • The opening curly brace { is the starting of the main function.
  • The next line inside the brace is a comment. This is a single line comment which starts with // and ends with the end of the line. A comment is not executed by the compiler not it is a part of our program. Comments are just used to make the program more readable and understandable.
  • In the next line we are actually printing the message with the ‘cout’ object. Strings in C++ are enclosed within double quotes.
  • return 0 is the ending point of our program. As the main function has declared of return type ‘int’, it has to return an integer value. But our program is not returning anything. Therefore a statement return 0 is needed.
  • The closing brace } is the ending of the main function. When this program executes, a message “Hello World” will be displayed on the output screen.

Note:- Every statement in C++ should end with a semicolon (;). As you may have noticed, we have put a semicolon at the end of every statement in our program.