×

Hello World Program in C++

The steps for “Hello World” C++ program are as follows:

  1. Write a C++ code given below in an editor.
  2. Save the file with .cpp
  3. Compile the code using C++ compiler or using online IDE.
  4. Check the output of the code.

“Hello World” program is considered as most basic and first program while learning any technology or language. In this program, user only prints “Hello World” statement to the screen.

Code:

// "Hello World" Program in C++
#include<iostream>
using namespace std;
int main()
{
     cout<<"Hello World";
     return 0;
}

Output:

Hello World

Let us understand the above program line by line

  1. // "Hello World" Program in C++
    • In C++, any statement starts with ‘//’ is called a comment line.
    • Comment line is not taken into consideration by compiler i.e. compiler don’t consider this line while compilation.
    • Comment is generally used to specify author name or to specify the meaning/logic of any statement.
    • In C++, comments are given to single line statement and multiline statements.
      1. Single line comments // statements
      1. Multiline comments /*      statements */
  1. #include<iostream>
  • The statement starts with (#) are called as directives in C++.
  • Here, #include tells compiler to add include the header file which is “iostream.”
  • The iostream header file provides inbuilt input/output objects and functions. We can use the “cout” object only by including “iostream” header file.

  1. using namespace std;
  • This statement add the “std” namespace into the current file.
  • Add an entire “std” package sometimes consider as bad practice because it imports all declarations from namespace “std” that contains the declaration for the cout and cin objects. Hence “std::cout” is consider as a good practice to use in program instead of importing entire “std” namespace into the code.
  1. int main()
  • In “int main ()” defines the function definition where main () is the function and int is the return type of function.
  • The main () is consider as the starting point of the program and compiler starts the execution from the main () function only. And hence it is important to write main () function in every C++ program.
  • As the return type of main () function is int, function will return an integer value from the function and the value that this main () functions return we see that in further article.
  1. { and }
  • The statements written inside these curly brackets { } are called the function body.
  • Here, “{” represents the starting of the function and “}” represents the closing of the function.
  1. cout<<"Hello World";
  • This statement tell compiler to print "Hello World" to output screen.
  • Here, cout is the object of ostream class which we included I current program using “iostream” header file.
  • The cout object is used to print the statement on the screen and it must use with << operator.
  • The cout print the statement to output screen using << operator and it print the string written inside the " " double quotes.
  • The above statement cout<<"Hello World"; is terminated with semicolon which represents the end of the statement. In C++, every statement must be terminated with semicolon (;).
  1. return 0;
  • This is also a statement in C++ and hence it is also terminated with semicolon (;).
  • To return a value from the function, return statement is used in C++.
  • The “return 0” statement is returning value 0 which is of type int and this int type is similar to the function return type.

Related Topics

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

CPP Templates

C++ provides a powerful feature called template, which allows the definition of generic classes and generic functions. Generic programming is a technique where different algorithms work in communion by using...

4 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

C++ Friend function

A friend function has the right to access all private and protected members of a class although it is defined outside that class' scope. Syntax class className{     ......     friend retyrn_type function_Name(argument);     .......   }   return_type function_Name(argument){     ......   } C++ friend function Example #include <iostream>   using namespace std;   class Length   {       private:           int meter;       public:           Length(): meter(5) { }           friend int addMethod(Length); //friend function declaration   };   int addMethod(Length l) // friend function definition   {       l.meter += 10; //accessing private data from non-member function       return l.meter;   }   int main()   {       Length L;       int totallength;       totallength=addMethod(L);       cout<<"Length: "<< totallength;       return 0;   } Output: Length: 15   C++ friend function...

1 minute read.

Reverse an Array in C++

The many approaches to reverse an array in the C++ programming language will be discussed in this section. The term "reverse of an array" refers to changing the order of...

8 minutes read.