Get started with Visual Studio 2017

Debugging

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.

Setting breakpoints

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.

Setting a breakpoint

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.

Starting the debugger

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.

Examining code

Stepping around

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.

Current statement in the debugger

You can use the following commands to step through your code:

Command Toolbar icon Description
Continue Continue icon Resumes code execution until the next breakpoint is hit
Step Into Step Into icon 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 Func1(Func2()), the debugger steps into the function Func2. If the current line doesn’t contain a function call, Step Into runs the code then suspends execution at the next line of code.
Step Over Step Over icon 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 Step Out icon 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.

Learn more

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