Last Updated:

C++ Program - Lesson 1

As a development environment for Windows, the author advises using Visual Studio 2008 (800 MB) if you have Windows XP, Visual Studio 2013 (6.5 GB) for Windows 7 and higher, or Dev C++ 5 (41 MB) for those who have too slow Internet to download Visual Studio. Users of Unix-like systems will need the GCC compiler and any text editor, such as Vim.

After you have installed everything you need, let's start writing the first program.


To create a project

For Visual Studio

Open the File menu → New → Project. Click the General tab and select Blank Project. Give the project any name, such as "lesson1" and click OK.

In the Solution Explorer window (usually in the upper-left corner), right-click the Source Files folder. In the dialog box, select Add → New Item. Enter a name for the new file— and click Add.main.cpp


Create a blank file and open it with any text editor that has syntax highlighting.

Code of the first program

Type the following code:

#include <iostream>
#include <cstdlib> // для system
using namespace std;

int main() 
    cout << "Hello, world!" << endl;
    system("pause"); //  MS Visual Studio
    return 0; 

Syntax description

The directive is used to connect other files to your code. The string will be replaced with the contents of the file "iostream.h", which is located in the standard language library and is responsible for entering and displaying data on the screen.#include#include <iostream>

#include <cstdlib> connects the standard library of the C language. This connection is necessary for the .system

The contents of the third line indicate that we are using the default namespace named "std". Anything inside the curly braces function will be automatically executed after you run the program.using namespace std;int main() {}

The string tells the program to display a message with the text "Hello, world" on the screen.cout << "Hello, world!" << endl;

The statement is designed to display text on a command-line screen. Two angle quotation marks () are placed after it. Next is the text that should be displayed. It is placed in double quotation marks. The operator moves the string to a lower level.cout<<endl

If there is any failure during execution, a non-zero error code will be generated. If the program terminated without failures, the error code will be zero. The command is necessary in order to send a message to the operating system about the successful completion of the program.return 0

- A semicolon is placed at the end of each command.

Compile and run

Now compile and run the program. Those who use MS Visual Studio need to press the key combination "Ctrl+F5". GCC users need to run the following commands:

c++ filename.cpp -o output_binaryname # compile code
./output_binary_name # start the program

If the program came together the first time, then fine. If the compiler says there are errors, then you have done something wrong.

Read the text of the error and try to correct it on your own. If it does not work, write about your problem in the comments.

As a homework, redesign this program so that instead of the message "Hello, World" the message "Hello, User" is displayed.