C++ "Hello, World!" Program

C++ProgrammingServer Side Programming

C++ is a general purpose programming language that supports procedural, object-oriented and generic programming. C++ is a superset of C and all valid C programs are valid in C++ as well.

C++ supports object oriented programming with features such as data hiding, encapsulation, inheritance, polymorphism etc.

Let us see the first C++ program that prints Hello, World!.

Example

#include <iostream>
using namespace std;
int main() {
   cout << "Hello, World!" << endl; // This prints Hello, World!
   return 0;
}

The output of the above program is as follows −

Output

Hello, World!

The different parts of the above program are explained as follows.

Headers

There are different headers in C++, each of which contain information that is necessary in the program. The header is used in this program which provides basic input and output services for C++ programs.

Namespaces

Namespaces are a relatively recent addition to C++. The following line we saw above informs the compiler to use the std namespace −

using namespace std;

main()

The program execution begins with the following line as the main() function is the entry point of any C++ program.

int main()

Output

The message “Hello, World!” is displayed on the screen using the following statement −

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

Here, cout is an object of the class ostream and is associated with the standard C output stream stdout.

Comments

Single line comments in C++ begin with //. They are used to make the program easier to understand and are ignored by the compiler. The following comment in the above program is to clarify the purpose of the cout statement to the programmers.

// This prints Hello, World!

return

The termination of the main() function is signalled by the return(0); statement. After this, the value 0 is returned to the calling process.

raja
Published on 26-Sep-2018 12:06:01
Advertisements