Create your own game
eng   рус

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 learning 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 a 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 its kind of mandatory. But another part of the program is the same. The thing is: we can have the 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 the 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 name of the function. And it's the entry point of the program. When we run the executable file, operating system looks 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 the compiler to check std namespace and find there cout. << is the insertion operator. It takes the right part and places it to the 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 the 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++

The first thing we need to learn is comments. Let's see the 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 you can put remarks for your teammates or for yourself for the future. A 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 the 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.

Conclusion

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

Exercises

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

Comments:

No comments yet