C++ Hello World Program


This article illustrates and shows how to build the classic hello world program in C++.

The Hello World program is a program that outputs the string "Hello World".

It is considered one of the most basic programs when first learning a new language.

Even though it's basic, it demonstrates a lot.

It shows, first of all, that you can get a program executed that can show output.

The "hello world" program in C++ is shown below.

#include <iostream>

int main()
cout << "Hello world";
return 0;

If you copy and paste the program above into a C++ compiler, it should compile and output "Hello World".

Below is a screenshot of the "hello world" program.

C++ hello world program

The output from this program is shown below.

C++ hello world program output

We'll break down this program into components which show how it works.

The first line, #include , is a header file which includes the iostream class.

The iostream class is the class which includes the objects, cin and cout, which are needed in order to input data or output data in C++. In this program, we use cout to write the line, "hello world". Without this header file, iostream, C++ cannot output anything because it would not recognize the cout object. Therefore, this header file is essential for the program to work.

The next line, int main (), is the main function of the circuit. Every C++ program requires a main function. When compiling a C++ program, the compiler must see a main() function. This function allows for a program to be executed. The main function is initialized as an int, meaning an integer. This means that if the function itself returns a value, it will be an integer.

Cout is an object of the iostream class that allows us to output information to a serial monitor screen. After Cout, 2 less than symbols, <<, are used. The string "hello world"; is then entered in after this. All strings must be placed in double quotation marks (" ") and all end with a semicolon.

If you want the string statement to add a new line after you enter in a statement, then you would put the line:

cout << "Hello world!" <

This starts a new line so that you will see the phrase, Press any key to continue . . . on a line below the "Hello world!" statement.

Below is the screenshot of the hello world program with a return line added.

C++ hello world program with a return line

The last line is, return(0); Although this line is absolutely needed, it is good practice to include a return statement in a C++ program with a main () function initialized to an integer.

Other than this, this is what it takes to output a string in C++.

Related Resources

How to Install Dev C++ Software

HTML Comment Box is loading comments...