C++ “Hello World” Program

hello_world_cpp

This is a simple “Hello, World!” C++ program to display “Hello World” text on the computer screen or display device.

Typically, the best way to learn programming is by writing code. The first program beginners write is “Hello, World!” which is often used to illustrate the syntax of a programming language. You can take a look at the list of Hello World Programs in 300 Programming Languages to see how “Hello, World!” looks like in different programming languages.

Let’s take a look at how “Hello World” program looks in C++.

Example 1: C++ Hello World Program

//My first C++ Program

#include <iostream>

using namespace std;

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

Output of C++ Program

Hello, World!

The first line of this program is a comment which is indicated by double forward slashes i.e.

//

Then lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor in C++.

Every C++ program run from the main() function regardless of where the function is actually located within the code.

The

cout

  is the standard output stream which prints the “Hello, World!” text on the computer screen.

Finally, the

return 0;

  is the exit status of the program which causes the C++ program to terminate.

Example 2: C++ Hello World Program

#include <iostream>
using namespace std;

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

Output of C++ Program

Hello, World!
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post