Writing the first program in C++

In a previous article Installing 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. In strings 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 { } our teams are. That's a record in a row 9function setlocale(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. Well, at the end of the block function 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 should be correct. Well, if there are no errors in 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 :)

Newsletter lessons:

Agree to receive notifications from purecodecpp.com to my e-mail

Writing the first program in C++

18 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. Там L маленькая в конце, endl это сокращение от end lineконец линии.

  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?

Leave a Reply

Place code in tags: <pre class="lang:c++ decode:true ">YOUR CODE</pre>