C++ Hello World program example

C++ hello world program example. This is very short hello world start up program. This C++ program will print hello world on console.

Before explanation about this start up program, first let’s see how a simple hello world program in CPP looks like.

 


// Hello world program in C++
#include<iostream>
using namespace std;

int main()
{    

      cout<<"Hello World!!!";     

      return 0;
}

 

C++ Hello World statements Explanation

#include<iostream>

We must include <iostream> header file / C++ library file by using #include pre-processor, so, we can use objects like cin and cout etc. to read data from console / user or print data on console.

In C++, predefined “cout” object of ostream class with insertion operator (<<) is used to display contents on console.

 

using namespace std;

In the C++ program, we need to write “using namespace std;” statement because the built in C++ library routines are kept in the standard namespace like cout, cin and string etc.

if we don’t use this statement in the program then we need to call cin or cout like std: cin or std::cout or std::string etc. So, to avoid the prefix std:: , it is better to write using namespace std; at beginning of source code.

 

int main () :

Every C++ program should contain main function because this is the entry point of the program. When we execute the program, compiler find the main () function and start executing from here. writing int before main () shows that function should return an int value.

 

return 0;

In main function, return 0  and says the operating system that program is completed successfully. zero(0), means exist status.