The basics of programming in c++ for beginners

Writing the first program in C++

In a previous articleInstalling the Microsoft Visual Studio environment 2015 Express (IDE)   we with you learned how to create a project in the development environment and add to it a new element (файл с расширением .cpp). Exactly in this file we will give clear commands to our computer.

Unfortunately, the computer can not understand us– it understands only one language –  machine code. but program thus it is impossible, many programming languages have been created. One of these is the C++ language. This language has a certain number of special words reserved logical, whereby in Microsoft Visual Studio(as with any other development environment) you can write commands to our computer. And then using the built-in compiler, this code will be converted into machine code, that will be processed, and we will show the result of the work program.

Well, start writing program. To do this, you need to open your created project. You go in the Microsoft Visual Studio. Before you open Home.

написание первой программы на c++

Here is there, where it is written last, must be a newly created project – click on it. If you have not yet created projects – create, as shown in this article. Start to write the following code. Strongly recommend not copy code, and manually type. Without practice in programming it is hard…  ))

Let's understand now, what we wrote. In 1-St and 2-nd lines are comments to our code. The entire text of the line, what is behind the double slash –  //  – completely ignored by the compiler. It simply does not see it. So we can anywhere in our code to leave comments – any information, which we deem necessary. Something like notes, to remember or pay attention.  Comments there are multiline, because if a comment is big, It is uncomfortable in front of the beginning of each line a double slash.  To create a multi-line comment, need a whole text or code, you want to ignore, placed between /*   and*/

More about the rules and coding standards, read our article Formatting source code.

In string 4 of program we see#include <iostream> – this preprocessor directive. While we will not delve into – just write so each new program.  The same applies to the line 5 – this is, the so-called, namespace connection std that contains the word commands, that we will use when writing each program.

Lines 7 – 14 located the main function: main ( ) { /*program code * / } When you run the program, it is always executed first and it between the two braces { } are our commands. Here's an entry in a row 9functionsetlocale(LC_ALL, “rus”); – will properly display on the screen Cyrillic signs. Prescribe the function in those programs, where you will be required to display the Russian text.

Moving to a string 11 – Here we entered the command cout (keyword from namespace std), which is responsible for data output to the screen, operator << followed by the text. The text should be placed in quotation marks. So we show the compiler, that it is not a command for it, and the usual string. In line write any text. When the text is written, quotation marks are closed again and write the operator <<  for which again the compiler command – endl;  (the transition to the next line).  Semicolon ; to put definitely. It said tocompiler, the command finished and can proceed to the next command.

At the end of the function block main()  visible command return 0;  It said tocompiler, that at the end of the programme it is necessary to return a value 0. Also didn't hesitate so far on how, what does it mean. Think of it, as a kind of rule – so it is necessary to write in each program. Any of our program in the following lessons, will be contain such records:

Use it as a template, before writing new programs.

How to run our program, to see the text on the screen, which has been placed in the source quoted? To do this, press Ctrl + Shift + B-will begin compiling a program.

If you make some mistakes in the code, Ddbugger to detect them and notify you. At the bottom of the window you will see a list of these errors. They need to be corrected. Well, if there are no errors on the bottom line of the window we see Assembling: successfully: 1, with errors: 0 etc.  Then press the Ctrl key + F5 and see in the window that opens, a message is, that requested:

написание первой программы на c++

The program has worked and performed our instructions properly. If you have any difficulties or errors, that you can not fix yourself, ask questions in the comments to this article. Together, we will look :)

If everything is clear and you are not tired, proceed to the next lesson– Data types, variables and constants in c++. And if you are tired, it still does not stop and watch related videos :)

34 thoughts on “Writing the first program in C++

  1. Thank you all turned himself is understood it was the Net Framevork 4 I had previously installed a later version that was the reason

  2. good afternoon, I ask this question, Error manually and left the team in scoring 11 row (example), I write end1; , the program does not want to take, indicates this error, and if the copy text from the example, everything goes.
    can you tell, that should be put after the end? There 1 or any other sign?

    1. A, all, Thank you, I have found that there is a Latin l(L).
      A little hasty with the question :)

    2. endl а не end1. There's a small L in the end, endl is an abbreviation of the end line – end of line.

    3. Not 1, а латинская строчная буква “l”, not “джи”, and “эл

  3. good afternoon! With what may be the lack of error is connected ucrtbased.dll libraries when the program starts ? and how to remove it?

  4. Hello! Why in the text written by one, and in another video. For example: on the text written, using namespace std, and video using std:cout и using std:endl

    1. Because it syntax rules C (check out the tutorials):
      – using namespace … – determines visibility all names from namespace std;
      – а using std:cout, for example, – determines visibility one particular behalf (cout) from namespace std;

  5. I after compilation, everything is working…but the console immediately closes.
    I use the command system(“pause”); then everything is OK, as in your example,.
    Question : how to make. so that the console is not closed and / or is always necessary to use this command?

Leave a Reply

Your email address will not be published. Required fields are marked *