C++ Tutorial: Hello World

The pinnacle program of programming languages is a “Hello, World!” program. The point of this program is to output “Hello, World!” on runtime and is used as the beginning program for many programming tutorials. This section assumes that you have a working compiler and that you know how to use it. Although any compiler should work with this program, this example will be using the Unix C++ compiler, c++. On my computer and on most Linux compilers, c++ is linked to g++, the GNU C++ compiler. Now, enough talking. Here is the program:

#include <iostream>
using namespace std;

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

After you saved this program in a file, you can compile and execute this program. Here’s how you do it on Linux:

c++ HelloWorld.cpp
./a.out

Replace HelloWorld.cpp with the name of your file.


That program may have looked a bit intimidating and complicated. Here is a breakdown of the program:


#include <iostream>

This is a preprocessor directive. This instructs the C preprocessor (cpp on Unix) to copy code from the iostream header into the program. The iostream header, given its name, contains programs for input and output, and is the source of the cout and endl we used in the program.


using namespace std;

This instructs the C++ compiler to use the standard namespace. I lied in the last line: #include <iostream> doesn’t give cout and endl, it gives std::cout and std::endl. As you can see with that, the format for namespaced objects is <namespace>::<object>. What using namespace std; does is that it makes it so you don’t have to type the std:: before all of the std-namespaced objects, as it takes the objects and puts them into the main namespace, which the program is in. All standard library objects are in the std namespace. We could have skipped that line and done the following program, and it would work just the same:

#include <iostream>

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


This is a blank line, it does nothing.


int main() {

This it the function signature for the main() function. In C++, the main() function is what is executed when the C++ program is ran. It has to be int at the beginning because the function must return an integer – the exit code of the program, which we will get to later. The curly brace marks the beginning of the function.


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

This is where the output actually happens. cout stands for console output, which used to be the output on the screen in the 20th century, when the C++ language was made. A way to think of it would be that cout is the screen/monitor/source of output and the arrows signify that "Hello, World!" and endl are going into cout. The actual meaning of this operator is a binary shift, but the cout object rewrites that meaning. The endl is a stream manipulator, something that manipulates the output. endl stands for end line, which makes it go to the next line. There is little difference in replacing this with the following statement:

cout << "Hello, World!\n";

The differences are beyond the scope of this article in the tutorial. The ; at the end signifies that it is the end of the statement, marks the end of most C++ lines of code. This means that the program could be fit into only 2 lines:

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

However, this is discouraged as it makes the program hard to read. Note that preprocessor directives must be on their own lines, as they don’t use semicolons.


return 0;

This states the exit code for the program. An exit code of 0 means that the program exited successfully. There are several other exit code, which will not be discussed in this article, for the sake of simplicity.


}

This line of code marks the end of the main() function. In this case, this is the end of the program.


I hope you found this “Hello, World!” guide meaningful and helpful. I will try to post more tutorials soon for C++ and for other languages,