Hello World in C++

Published on 21 March 2018 (Updated: 15 May 2023)

Welcome to the Hello World in C++ page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#include <iostream>
using namespace std;

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

Hello World in C++ was written by:

This article was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

Even though C++ was built on C, the implementation of Hello World in C++ is slightly different.

It appears this implementation of Hello World may be competing with Java for hardest to learn. But at any rate, let's break it down.

Once again, the first line is an include statement. Here, we're including the IO stream code in our solution. This is how we gain access to the IO objects like cout.

Next, we gain access to the std namespace which basically allows us to shorten std::cout to cout. It's really just a style thing that eliminates a lot of verbosity that you might get with other languages like Java. Of course, if another namespace also has a cout, we'll run into problems.

After that, we make a main method as usual. This is exactly the same as C, so I won't bother explaining the return type bit again.

Finally, we write our string to the cout stream. The syntax is a bit strange, but basically we can imagine that the Hello World string is inserted into the cout stream. In fact, the double-arrow operator is the insertion operator, and it has some fun properties. For instance, the operator can be chained together, but that's a topic for another time.

How to Run the Solution

Perhaps the easiest way to run the solution is to leverage the online gdb compiler.

Alternatively, you can try to run the C++ code in a similar way described in the last article. Honestly, it's pretty easy to write and run C/C++ code on most platforms:

gcc -o hello-world hello-world.cpp
./hello-world

Unfortunately, Windows pretty much requires the use of Visual Studio. So, instead of sharing platform specific directions, I'll fallback on my online compiler recommendation.