Hello World in C++

By TechCogs | Programming and Coding | 3 Mar 2020


C++ is truly a historic language.  Based heavily on C, most client applications, especially in embedded systems and commercial software, are still written in C++, and the language continues to evolve and add new features.  C++ compilers have become much more user friendly, especially on Linux.

The classic Hello World program in C++ is fairly simple.

#include <iostream>

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

The include line tells the preprocessor to include the iostream module in this code file, giving us access to everything it contains.  Because the name of the module is surrounded by angle brackets <> we know that the library is part of the overall system's libraries.  Local libraries that are specific to a particular project are surrounded by quotes.

Next the main function definition should be fairly straightforward.  This is where the program begins and it returns an int when done.

Next we look at the "std::cout" call.  The "std::" part of that call means that cout is contained in the std namespace.  Namespaces are a way to organize code and keep similarly-named things from conflicting with each other.  For instance, if you have two functions that do two different things but have the same name, but each is in a different namespace, they won't conflict with each other.

If you're only going to use one namespace in a program you can simplify your code by letting the compiler know which namespace you're going to use, like this.

#include <iostream>

using namespace std;

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

This allows you to omit having to begin every call to a function or object in the std namespace with "std::".  This can be very convenient and save some typing, but it's still good to use namespaces not only to make it clear which namespaces all the functions and objects are in but also to maintain the highest amount of compatibility throughout your code.

Then we use the "<<" operator to send our Hello World text to the cout interface, which prints it to the terminal or command line.  Finally we add "std::endl" on the end which adds the appropriate end line characters for the platform the code is being compiled on.

Adding endl could be skipped and instead you could write the text as "Hello, World!\n" using the standard newline character, but using endl ensures the greatest compatibility for the code when compiled across different platforms.

There are many, many books and articles written about C++, but the classic Hello World program is simple and gives you a look at a few of the basics of the language.

How do you rate this article?

9


TechCogs
TechCogs

I work with Linux, technology, computers, and science.


Programming and Coding
Programming and Coding

The wonderful world of software engineering and software creation, centered around languages like D, Java, Python, Dart, and C++.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.