C++ “Hello, World!” Program

Printing “Hello, World!” is the first program in C++. Here, this prints “Hello, World” on the console (output screen). To start learning C++, it is the first step to print sometime on the screen.

C++ Program to Print “Hello, World!”

Let us see the first C++ program that prints “Hello, World!” −

// First C++ program 
#include<iostream>
using namespace std;

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

Output

This program will print “Hello, World!” on the output screen. The output will be −

Hello, World!

Parts of C++ “Hello, World!” Program

Here is the breakdown of the above code and all elements used in the above code −

1. Comment Section (// First C++ program)

Comments are used to specify a textual line that is not supposed to be executed when we compile the code. The compiler ignores the line, and proceeds to the next line. These are used for better readability and explanation of code in the comments section.

This is the comment −

// First C++ program

2. Preprocessor Directive (#include <iostream>)

The #include is known as a pre-processor directive in C++. It is used to include header files with specific methods and elements. Multiple #include statements are used to apply different header files in the program. The iostream is the header file that defines functions and operations related to the input/output stream.

The statement is used in the program is −

#include <iostream>

3. Namespace (using namespace std;)

Namespaces are used to differentiate code blocks with the same method names. In this program, the using namespace std; is used to set the namespace as standard for users to apply all standard methods in programs.

Here is the code statement used in the program −

using namespace std;

4. The main() Function (int main(){…})

The main() function is the default starting point of any C++ program. It is compulsory for any C++ program to have a main function. The program logics are written inside the main program. The main function body is enclosed inside parenthesis ({}).

The main() function part is −

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

5. Printing Statement (cout)

The print/output statement is cout followed by ”<<” operator. This is used to print the given parameters specified in the statement on the screen. We can also print multiple elements in a single cout block.

The print statement is −

cout << "Hello, World!";

6. Return Statement (return 0;)

The return statement is also known as the exit statement. It is used to exit from the corresponding function. The “return 0” is the default statement to exit from the main program.

Here is the return statement used in the program −

return 0;
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team