Press ESC to close

How to print Hello World in C++

Photo by luis gomes

This is a simple “Hello World” program. This will print “Hello World” in output screen.

#include <iostream>  
using namespace std;  
int main() {  

    cout << "Hello, World!" << endl;      

return 0;  

}

Description

  1. Preprocessor Directive:
    • #include
      • #include is preprocessor directive. It is used to add header file in a program. Iostream is a header file that stands for input output stream. With the help of this header file we can use cin, cout statements in the C++ program.
  2. Namespace Declaration:
    • using namespace std;
      • The std namespace includes all the features of the C++ Standard Library. By including this line, we don’t need to prefix standard library names with std::.
  3. Main Function:
    • int main() { … }
      • The main function is the starting point of every C++ program. It returns an integer value, which is typically used to indicate whether the program ended successfully.
  4. Output Statement:
    • cout << “Hello, World!” << endl;
      • cout is an object of the iostream library used for outputting data to the console. Since we declared using namespace std;, we can use cout directly without the std:: prefix.
      • << is the stream insertion operator, which inserts the string “Hello, World!” into the output stream.
      • endl is an object of the iostream library that represents a newline character, ensuring that the output cursor moves to the next line after printing the message.
  5. Return Statement:
    • return 0;
      • This statement ends the main function and returns the value 0 to the operating system. A return value of 0 typically indicates that the program executed successfully.

Leave a Reply

Your email address will not be published. Required fields are marked *