C++ Hello World Program with Code Explanation

Hello World Program in C++

The “Hello World” program is the first but most vital step towards learning any programming language and it is certainly the simplest program you will learn with each programming language. All you need to do is display the message “Hello World” on the output screen.

Let us now look at C++ Hello World Code:

Step 1) Select create the cache now option.

You should see a screen something like this

In some computers and operating systems, it asks whether to include all the libraries. If the option is selected, it will install all the libraries.

Step 2) Once the program opens, you need to create a new source file, so you can start writing your first C++ program. To do this select File > New > Source File. The path is shown in the figure below.

This will open an area where you be able to type out your code.

Step 3) After that you can write the C++ code as shown in the image below:

C++ Code Example:

#include<iostream>    
using namespace std;    

int main()    
{    
    cout<<"Hello World"<<endl;    
    return 0;    
}    

Step 4) In this step,

Click on Execute->compile & run

Step 5) Save the file

After saving you should a black screen outputting “Hello World.”

Your First Program: C++ “Hello World!” Explanation

C++ is a compiled language. The source code is compiled into object files. Object files are then combined by a linker creating an executable program.

A production C++ consists of many source code files (usually called source files).

  • Curly braces, {}, express grouping in C++. Here, they indicate the start and end of the function body.
  • Every C++ program has exactly one global function named main (). The program starts by executing that function. An int value is returned by main (), which it passes to the system.’ If no value is returned, then the system will receive a value 0 thus indicating successful completion. A nonzero value from the main () function indicates failure.

Explanation of C++ Hello World Program Code

Code line 1: The first line is #include <iostream>. It instructs the compiler to include the standard stream I/O library. Without this header inclusion the expression would not compile.

std::cout << "Hello, World"<<endl

Code line 4: int main(). This is the main function of the program. Functions are denoted by the parentheses(). Before the main function is “int”. This means that the main function will return an integer to the function or process that called it.

Don’t worry about this, for the time being, simply note that the program must return an integer before the end. The curly braces, { and }, contain the code within a function. The program terminates, at the end of the main function denoted by }

Code line 6: The operator << writes its second argument onto its first. In this case, the string literal “Hello, World!” is written onto the standard output stream std:: cout.

(Note: A string literal is a sequence of characters surrounded by double quotes. endl inserts a newline character on the same line)

Code line 7: return 0; This is the last command in the main function, the return statement. Its purpose is only to return a value to the function or process that called the main function. Don’t worry about this other than the fact that it is required by the “int” in front of the main function definition. It should return a zero from the main function meaning the program ran successfully and exited.

cout<<"Hello World"<<endl;

Note: Cout is a stream which outputs to the stream specified. It is by default the standard output stream. Cout is very common in programs as the ultimate motive in every program is to give some output. endl; represents the end of statements in C++. The semicolon in C++ separates different statements and must be put at the end of statements in C++.

Summary

  • The “Hello World” program is the first step towards learning any programming language.
  • After installing a C++ compiler and a Text Editor of your choice, you can go ahead and execute your first basic C++ program.
  • The first line is #include . It instructs the compiler to include the standard stream I/O library.
  • : int main(). This is the main function of the program.
  • The operator << writes its second argument onto its first.
  • Return 0; is the last command in the main function which is the return statement.
  • : Cout is a stream that outputs the stream specified.