Something about programming

Hello World in C++

Previous tutorial: How to compile C++ code with g++
Next tutorial: Data Types and Variables in C++

It's a tradition to start learing programming language with the Hello World program.

Let's start with code right away. We have two versions of Hello World. One for GCC (or any other compiler) and one for Visual C++ 2017. For GCC the code can look like this:

#include <iostream> int main() { cout << "Hello World\n"; return 0; }

If you create project in Visual Studio, you'll have one small difference:

#include "pch.h" #include <iostream> int main() { std::cout << "Hello World!\n"; }

Visual Studio adds this first line to every project and it's kind of mandatory. But other part of the program is the same. The thing is: we can have same code for different operating systems. All we need to do is to compile the code to the operating system we want.

#include is a directive. It tells compiler to insert in this place the specific file. pch.h and iostream - are the names of the files. We include so called header files - we discuss what they are in the future tutorials. We include header files to be able to use some code. We can use cout only if we include iostream. iostream allows us to use input and output in our programs.

Then we see int main() - every program must have this line. main - is the the name of the function. And it's the entry point of the program. When we run executable file, operating system look up for main and executes code in curly braces after the name. The syntax should be always the same: int main() then curly braces and in curly braces we write our program. Parentheses after the main are empty now, but they may contain something too - we'll discuss it later.

To print the line we need to use cout. std - is the namespace there cout belongs. So std::cout - we tell compiler to check std namespace and find there cout. << is the insertion operator. It takes righ part and places it to left part. So it takes "Hello World!\n" and puts it in cout. And as result we see text Hello World! on the screen. Pay attention that we use double quotes for text and also there is special character \n - it is not visible on the screen but it tells console to break the line. At the end of the line of code we put semicolon.

Last line returns control to the environment and program ends.

Comments in C++

First thing we need to learn are comments. Let's see code:

/* Hello World.cpp This program prints some text */ #include <iostream> // includes iostream // Main function of the program int main() { cout << "Hello World\n"; //print text return 0; // end of the program }

I've added several comments. Comment is the text that compiler will ignore - so we can put there remarks for your teammates or for youself for the future. Comment that starts with // works till the end of the line. /* */ is the block comment and it can have several lines.

Standard namespace

Now we need to put std:: before any entity from standard namespace and we'll use standard namespace a lot. We can point out that we'll use standard namespace by default. To do this we need to put using namespace std; before main:

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

Now we can use just cout (and any other symbols from standard namespace). Later we'll use our own namespaces. And we'll talk about namespaces in future tutorials.


We created our first program. Now you need to compile it. You can find needed information in tutorials from the Software partition.


  1. Compile and run the Hello World program
  2. Output another text string


No comments yet