Why use Visual Studio 2015?
It is Chinese, user-friendly, auto-complete, real-time syntax error hint (middle Wave line part), Single step debugging ... The most important Community edition is free ! So you don't have to use your conscience again. Vc++6.0,come to VS2015 and enjoy for illegitimate , old incompatible contemporary systems it!
Next I'll show you how to install and use it to write, run C + + programs!
?
Get Visual Studio 2015
Go to the official Visual Studio website and click "Download Visual Studio Community".
If everything works, you'll get an app that's about 3MB in size (I assume you know how to download it and know how to find the downloaded file):
Double-click it, wait a moment, and come to this interface:
Instead of modifying the installation directory, select " Customize " and click " next " to come here:
Just select "Visual C + +" under " programming language " and click Next to come here:
Check and click " install " to see this interface:
You can click " Minimize " to play the other, the installation process of about one hours, during the need to keep the network unblocked.
It's done! See that big " start " thing? Point!
To build a C + + program project in VS
Now, we can start the VS tour!
Open VS2015, which may let you log in:
Here you can log in if you like, I choose to " say it later ":
Here the development settings, I choose "Visual C + +", color I chose the cool " dark ". (according to the Taste of the line, do not have to make the choice of difficult, anyway can change later)
Everything is ready, " start visual Studio"!
Wait a moment and you'll see this interface:
Click menu: " file "-" new "-" project ", this window will appear:
After selecting "Visual C + +" On the left, select "Empty Project " on the right, the project name is "myfristcpromgram" and click " OK ". (Of course the project name you don't have to write as long as I do, just a few letters can be)
Then, in the Solution Explorer on the left, right-click header File , add , new Item . (If you don't have Solution Explorer on the left, you can click the menu: window -Reset window layout )
In the pop-up window, change the name of the new item to "main.h" (You can also use "main.c" or "main.cpp" or whatever name you like)
Now it's time to write your code:
Let's say hello to the world and write down the code:
Click the green button (" Local Windows Debugger ") of the triangle on the toolbar to run, or the quicker way is to press F5directly.
Tick "Do not show this dialog box again " and click " Yes ".
A flash of the past!
"Director, the script looks wrong!" "
When you click on the Run button or press F5 , the program flashes over, what's going on, VS2015 bug?
Not, we look at this program, this program only a row of printf, no other statement, the computer can be executed in a moment, the execution is finished, this is the reason for a flash.
But I want to see the output of the text ah, how to let it stop after printf ?
It is simple to use system ("pause"); command, like this:
You will notice that the system is drawn with a red wavy line, move the mouse over to find vs tells us that this is an undefined identifier because the system is in the same library that we did not include, we added this line Include:
By pressing F5, you have now learned how to use Visual Studio to write and run simple C + + programs:
Add
If you do not want to use system ("pause"); Include a library, or you can use the scanf function to wait for input, to pause the program, or you can set a breakpoint in the last curly brace, which we will explain in detail later.
Coach, scanf can't use it?!
"Vs's Problem how so much! You can't even write a C program with a scanf ? "
Wait, please calm down and tick " no longer show this dialog " click " no ", let's take a closer look at the output of this error message:
Error C4996: ' scanf ': This function or variable could be unsafe. Consider using scanf_s instead. To disable deprecation, use _crt_secure_no_warnings. See online Help for details.
Vs tells us that this function may not be safe and should consider using scanf_s instead (where it is unsafe, interested babies can look at the sections at the end of this section). Or define a _crt_secure_no_warnings macro to block this warning.
So the first way we can modify scanf to scanf_s, we can compile
It seems to work well!
No I want to use scanf! I want to use it! To use!! "
For this obsessive compulsive disorder, we can turn off the security warning of VS by defining the _crt_secure_no_warnings macro:
Both methods are optional, andscanf_s is still available after you close the VS security warning.
Not safe.
scanfSelected readings
When using scanf to read the string through %s , many beginners ignore the string length of the user input , causing the program to become unstable after the buffer overflow . Observe the following procedure:
First the program opens up a 5-character space to accept the input, but the problem is that you don't know how much the user will enter.
Let's run this program, enter a text that is significantly larger than 5 characters , and test it:
haha Nothing at all, you're scaring me! "
Don't be happy too early, press any key to end the program:
VS gave us a warning to tell us that the "str" variable stack is not normal, in fact, this is the term " buffer overflow " We mentioned earlier, when we put long data into a short position, the extra part will overwrite the program's other data, Sometimes the things that are overwritten are unimportant, the program works properly, sometimes it is important, the program crashes, which is called unsafe.
"But I didn't make this mistake when I was using vc++6.0 ?" "
I think it's because vc++6.0 is too old to have the ability to check this problem.
There is a more subtle form of this problem: what if we just typed in 5 characters ? such as "Hello".
In fact, the buffer overflows , because the string ends with the '/0 ' character and itself occupies a position, so "Hello" is actually 6 characters !
Use vs Auto-adjust code indentation
Often the code we paste from elsewhere is not indented correctly, or a lot of sloppy people who write code may never notice indentation, although indentation is not part of the C + + syntax, but it affects the readability of the program.
Like a procedure for judging the shape of a triangle, some children can write it like this:
Don't laugh, that's what I've seen. This does not really make a difference to the compiler, but if you want to modify this code, you have to spend at least three times times more effort than usual. For example, I ask you, which is the second-to-last, and what if? Can you see it at a glance?
Now we use the function of the auto-format code of VS, select the code to be formatted, which we select all (Ctrl+ a):
Hold down Ctrl, click K, click F, is it magical?
So now I'm going to ask you, what 's the second-to-last, and what if?
?
Use the VS breakpoint with the single Step tracking feature
Often, in order to find out the problem point of the program, we can set a breakpoint in VS and follow it step-by-step, observing the changes of each variable to find the wrong place.
Go back to the one that calculates the total square of the program , right-click the line for the loop, breakpoint - Insert Breakpoint . (or move the cursor to this line first, then press F9, or tap the blank in the beginning)
A red circle will appear at the header of the bank, and clicking on the red circle cancels the breakpoint .
F5 Run the program, for the scanf_s enter "ten" carriage return, the window will automatically jump to VS, the cursor is automatically positioned to the breakpoint line.
When you move the mouse over any variable name, you can see the value of the variable. Let's move to the variable i :
See that it has a weird value.
"No, ah, I am not assigned to the value of 0 ?" It's in the line! "
Yes, but the moment the breakpoint breaks is that the line is executed, and now I is not defined and initialized, its value is naturally indeterminate.
We click on this pin and will fix the value of the display I .
You can drag it to a comfortable location, and even you can manually modify its value to add comments, but here's what we don't need to do.
We press F10 to let the program take a step forward:
I immediately found the value of I as 0, and the VS is also shown in Red to us, while the cursor automatically moves to the if statement, the left side of the yellow arrow surface where the current program runs. Press F10again.
Since i satisfy the condition that the radical is still an integer, I enter the if branch and prepare to output this i.
All the way F10, you can observe clearly when the whole program I is increased, when the output.
When you do not want to track again, click on the left side of the red circle to cancel the breakpoint, press F5, let the program run itself.
?
2015-10-24
Wangzexi
Installation and use of Visual Studio 2015