×

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++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Difference between OOP and POP in C++

Object-Oriented Programming (OOP) Prioritizes data over methods (functions) and treats data as an essential component of program development.  OOP prohibits the free flow of data throughout the system. Tighter ties to data manipulation...

4 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.

Virtual class in C++

Introduction to Virtual Base class Virtual base classes can be utilized in virtual inheritance as a mechanism for examining many "instances" of a certain class while searching through multiple inheritances in...

6 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

C++ vs C#

What exactly is C++ programming? Bjorne Stroustrup is the creator of the C++ programming language. His goal was to create a powerful object-oriented programming language with the capabilities of C. It...

4 minutes read.

C++ Features

C++ is a general-purpose programming language that evolved from the C language to include an object-oriented paradigm. It is a compiled and imperative language. Object-Oriented Programming Object-oriented programming language concepts: ClassObjectsEncapsulationPolymorphismInheritanceAbstraction Class: A Class...

4 minutes read.

Principles of Object-Oriented Programming in C++

What is Object-Oriented Programming? Object-oriented programming is about creating obejcts that represent the real-world entity . In object-oriented programming, objects are created for the class. One of the main objectives of...

5 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Pattern programs in C++

In this article, we are going to discuss different pattern programs in C++. Program for Printing * patterns: Right Angle Triangle* pattern #include <iostream> using namespace std; int main() {     int rows;     cout <<...

3 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.