Get started with Visual Studio 2017
Visual Studio’s debugger can help you find problems in your code efficiently. The debugger lets you run your program and pause it before given lines of code are run so that you can inspect what your program is doing.
You can follow our Build a calculator with C++ tutorial to learn how to debug your code through a quick example.
When you’re debugging a program, it’ll pause at lines of code where you add a breakpoint, which acts as a stop sign for your code. When your program runs into a breakpoint and pauses, you’ll be able to inspect parts of your program to find problems in that area of your code.
To set a breakpoint, click to the left of its line number so that a red dot appears.
Starting the debugger
To start debugging, run your code using the green play button in the toolbar or by pressing F5. Your program will run until it reaches the first breakpoint that it encounters.
Examining variables in your code
When your program is paused, you can use features of the debugger to check the values of variables in your code. This way, you can make sure that they’re storing the values that you expect them to have. You can use your mouse to hover over a variable in its code to see the current value that it’s storing.
You can also use the Autos and Locals tool windows to inspect variables. The Autos tool window shows the type and current value of variables used on the current line or preceding line. The Locals window shows variables that are currently in scope. When you’re debugging, these windows appear in the bottom left of Visual Studio by default, but you can reopen them using the Debug → Windows menu.
When your code is paused in the debugger, you can walk through your code step by step to see what happens when each statement is run. When you’re stepping through your code, you’ll see a yellow arrow that shows the statement on which the debugger paused. This statement is the next to be executed.
You can use the following commands to step through your code:
|Continue||Resumes code execution until the next breakpoint is hit|
|Step Into||Runs the next statement. If the current line contains a function call, Step Into steps into the most deeply nested function. If you use Step Into on a line of code like
|Step Over||Runs the next statement without stepping into functions or methods. If the current line contains a function call, Step Over runs the code then suspends execution at the first line of code after the called function returns.|
|Step Out||Advances the debugger all the way through the current fuction. Step Out continues running code and suspends execution when the current function returns.|
Hint: Step Into and Step Over are your primary tools for stepping through code line-by-line. Use Step Into when you want to debug a method called by the current line of code. Otherwise, use Step Over.
Build a calculator with C++ - Learn to create and debug a simple C++ calculator app.
Video: Debugging your code - Introductory debugging video.
Navigating through code with the debugger - Learn advanced debugging commands
I ran into an issue Next: Customizing Visual Studio