C++ Hello World Program

In this tutorial, you will learn how to write, compile, and run a simple Hello World program in C programming and also will get to know the meaning of each line of code.


Before learning all the concepts of C++ programming, let us check a basic Hello World program that will print the “Hello World” message on the console. We will compile and run the program using a C++ compiler.

A C++ program can also be characterized as a collection of objects which communicate with each other by invoking each other’s methods. Let’s see what an object, method, class, and instance variable can mean.

  • Object – An object is an instance of a class and any Object has a state and behavior. For example, A cat has states like name, color, and breed. It also has behaviors like sleeping, eating, and purring.
  • Methods – A method is a behavior of an object and a single class can contain any number of methods. The data is manipulated and the logic is coded in the methods of a program
  • Class – A class is like a blueprint or a template that describes the different behavior and states of an object of the same class
  • Instance Variable – Every object has its instance variable and the state of an object is created by the values assigned to its instance variables.

We will discuss all these concepts in great detail in the later chapters. For the sake of simplicity, we will discuss a basic C++ program structure in this tutorial.

C++ Hello World Program 

The following code would print the simple output of “Hello World” in the console.

File: hello-world.cpp

//"Hello World" program in C++

#include <iostream>
using namespace std;

// The code execution begins from the main() function

int main() {
   cout << "Hello World"; // statement to print hello world
   return 0;
}

C++ Hello World Program: Without including namespace

//"Hello, World!" program in C++

#include <iostream>

// The code execution begins from the main() function

int main() {
   std::cout << "Hello World"; // statement to print hello world
   return 0;
}

Output

Both the above program produces the same output as below.

Hello World

Explanation

We will dissect the program line by line and see how it gets executed.

  • <iostream> is a header that contains many basic functions and necessary commands needed for program execution. It is one of the most common headers in C++ to be imported into almost all programs.
  • The second line using namespace std; informs the compiler to use the standard namespace and namespace is a recent addition to C++. We will go deeper into them in later chapters.
  • The next line which begins with // is a single comment line that is for user understanding and will not get executed.
  • int main() is the entry point of any C++ program and execution always begins from here. The lines enclosed within '{' and '}' are part of the main block and get executed.
  • The next line is cout<<"Hello World"; which causes the output text to be displayed on the screen.
  • The next line return 0; signifies the end of the function and returns the value 0 to the compiler which signifies that the execution completed normally without any error during execution.
  • The final line of '}' signifies the last line of the code and where the main block ends.

To execute this program, you either need an online C++ environment or install C++ in your local machine and then execute it in your IDE ( Integrated Development Environment ). Then you will be able to see the output on your screen after the code execution.

How to Run and Compile a C++ Program

You can write your first program in any text editor available on your computer and then save it with the extension of .cpp and execute it using the steps mentioned below. For Linux and windows based users we have divided it into two sections as follows.

Linux Based OS (Eg. Ubuntu)

  • Open any text editor and write the code above into a text file
  • Save the file as .cpp and open terminal
  • Navigate to the location of the file in the terminal
  • Type g++ first.cpp to compile your code and it will either show you the errors or just create an out file with no errors.
  • Now type out to run the program.
  • You will be able to see the output Hello World on your display screen.

Windows OS (Eg. Windows 10 Home)

  • You can download an editor for your code and some of the most popular IDE’s are Codeblocks and NetBeans.
  • Make sure that the C++ is installed and added to the PATH environment in your machine.
  • Then execute the code with the run command and it will execute the code and show the result in a separate window usually towards the bottom.

This is how you can compile and execute your code in your local machine and also debug errors and correct them.

Please get connected & share!

Advertisement