1 day ago c visual-studio-code. Improve this question. Follow asked 1 hour ago. 1 2 2 bronze badges. New contributor. Matey is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. In this video I am going to show, How to Set Up C Development With Visual Studio Code on Windows 10. We will use MinGW with VS code as our compiler and deb.
This article introduces the features of the Visual Studio debugger in a step-by-step walkthrough. If you want a higher-level view of the debugger features, see First look at the debugger. When you debug your app, it usually means that you are running your application with the debugger attached. When you do this, the debugger provides many ways to see what your code is doing while it runs. You can step through your code and look at the values stored in variables, you can set watches on variables to see when values change, you can examine the execution path of your code, see whether a branch of code is running, and so on. If this is the first time that you've tried to debug code, you may want to read Debugging for absolute beginners before going through this article.
Although the demo app is C++, most of the features are applicable to C#, Visual Basic, F#, Python, JavaScript, and other languages supported by Visual Studio (F# does not support Edit-and-continue. F# and JavaScript do not support the Autos window). The screenshots are in C++.
In this tutorial, you will:
- Start the debugger and hit breakpoints.
- Learn commands to step through code in the debugger
- Inspect variables in data tips and debugger windows
- Examine the call stack
Prerequisites
You must have Visual Studio 2019 installed and the Desktop development with C++ workload.
You must have Visual Studio 2017 installed and the Desktop development with C++ workload.
If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
If you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
If you need to install the workload but already have Visual Studio, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. The Visual Studio Installer launches. Choose the Desktop development with C++ workload, then choose Modify.
Create a project
First, you'll create a C++ console application project. The project type comes with all the template files you'll need, before you've even added anything!
Open Visual Studio 2017.
From the top menu bar, choose File > New > Project.
In the New Project dialog box in the left pane, expand Visual C++ and then choose Windows Desktop. In the middle pane, choose Windows Console Application. Then name the project get-started-debugging.
If you don't see the Console App project template, choose the Open Visual Studio Installer link in the left pane of the New Project dialog box. The Visual Studio Installer launches. Choose the .NET Core cross-platform development workload, and then choose Modify.
Click OK.
Visual Studio opens your new project.
Open Visual Studio 2019.
If the start window is not open, choose File > Start Window.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C++ from the Language list, and then choose Windows from the Platform list.
After you apply the language and platform filters, choose the Console App template, and then choose Next.
Note
If you do not see the Console App template, you can install it from the Create a new project window. In the Not finding what you're looking for? message, choose the Install more tools and features link. Then, in the Visual Studio Installer, choose the Desktop development with C++ workload.
In the Configure your new project window, type or enter get-started-debugging in the Project name box. Then, choose Create.
Visual Studio opens your new project.
Create the application
In get-started-debugging.cpp, replace all of the default code with the following code instead:
Start the debugger!
Press F5 (Debug > Start Debugging) or the Start Debugging button in the Debug Toolbar.
F5 starts the app with the debugger attached to the app process, but right now we haven't done anything special to examine the code. So the app just loads and you see the console output.
In this tutorial, we'll take a closer look at this app using the debugger and get a look at the debugger features.
Stop the debugger by pressing the red stop button (Shift + F5).
In the console window, press a key and Enter to close the console window.
Set a breakpoint and start the debugger
In the
for
loop of themain
function, set a breakpoint by clicking the left margin of the following line of code:name += letters[i];
A red circle appears where you set the breakpoint.
Breakpoints are one of the most basic and essential features of reliable debugging. A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.
Press F5 or the Start Debugging button , the app starts, and the debugger runs to the line of code where you set the breakpoint.
The yellow arrow represents the statement on which the debugger paused, which also suspends app execution at the same point (this statement has not yet executed).
If the app is not yet running, F5 starts the debugger and stops at the first breakpoint. Otherwise, F5 continues running the app to the next breakpoint.
Breakpoints are a useful feature when you know the line of code or the section of code that you want to examine in detail. For information on the different types of breakpoints you can set, such as conditional breakpoints, see Using breakpoints.
Navigate code in the debugger using step commands
Mostly, we use the keyboard shortcuts here, because it's a good way to get fast at executing your app in the debugger (equivalent commands such as menu commands are shown in parentheses).
Run C++ Program In Visual Studio Code
While paused in the
for
loop in themain
method, press F11 (or choose Debug > Step Into) twice to to advance to theSendMessage
method call.After pressing F11 twice, you should be at this line of code:
SendMessage(name, a[i]);
Press F11 one more time to step into the
SendMessage
method.The yellow pointer advances into the
SendMessage
method.F11 is the Step Into command and advances the app execution one statement at a time. F11 is a good way to examine the execution flow in the most detail. (To move faster through code, we show you some other options also.) By default, the debugger skips over non-user code (if you want more details, see Just My Code).
Let's say that you are done examining the
SendMessage
method, and you want to get out of the method but stay in the debugger. You can do this using the Step Out command.Press Shift + F11 (or Debug > Step Out).
This command resumes app execution (and advances the debugger) until the current method or function returns.
You should be back in the
for
loop in themain
method, paused at theSendMessage
method call.Press F11 several times until you get back to the
SendMessage
method call again.While paused at the method call, press F10 (or choose Debug > Step Over) once.
Notice this time that the debugger does not step into the
SendMessage
method. F10 advances the debugger without stepping into functions or methods in your app code (the code still executes). By pressing F10 on theSendMessage
method call (instead of F11), we skipped over the implementation code forSendMessage
(which maybe we're not interested in right now). For more information on different ways to move through your code, see Navigate code in the debugger.
Navigate code using Run to Click
Press F5 to advance to the breakpoint.
In the code editor, scroll down and hover over the
std::wcout
function in theSendMessage
method until the green Run to Click button appears on the left. The tooltip for the button shows 'Run execution to here'.Note
The Run to Click button is new in Visual Studio 2017. (If you don't see the green arrow button, use F11 in this example instead to advance the debugger to the right place.)
Click the Run to Click button .
The debugger advances to the
std::wcout
function.Using this button is similar to setting a temporary breakpoint. Run to Click is handy for getting around quickly within a visible region of app code (you can click in any open file).
Restart your app quickly
Click the Restart button in the Debug Toolbar (Ctrl + Shift + F5).
When you press Restart, it saves time versus stopping the app and restarting the debugger. The debugger pauses at the first breakpoint that is hit by executing code.
The debugger stops again at the breakpoint you previously set inside the for
loop.
Inspect variables with data tips
Features that allow you to inspect variables are one of the most useful features of the debugger, and there are different ways to do it. Often, when you try to debug an issue, you are attempting to find out whether variables are storing the values that you expect them to have at a particular time.
While paused on the
name += letters[i]
statement, hover over theletters
variable and you see it's default value,size={10}
.Expand the
letters
variable to see its properties, which include all the elements that the variable contains.Next, hover over the
name
variable, and you see its current value, an empty string.Press F5 (or Debug > Continue) a few times to iterate several times through the
for
loop, pausing again at the breakpoint, and hovering over thename
variable each time to check its value.The value of the variable changes with each iteration of the
for
loop, showing values off
, thenfr
, thenfre
, and so on.Often, when debugging, you want a quick way to check property values on variables, to see whether they are storing the values that you expect them to store, and the data tips are a good way to do it.
Inspect variables with the Autos and Locals windows
Look at the Autos window at the bottom of the code editor.
If it is closed, open it while paused in the debugger by choosing Debug > Windows > Autos.
In the Autos window, you see variables and their current value. The Autos window shows all variables used on the current line or the preceding line (Check documentation for language-specific behavior).
Next, look at the Locals window, in a tab next to the Autos window.
Expand the
letters
variable to show the elements that it contains.The Locals window shows you the variables that are in the current scope, that is, the current execution context.
Set a watch
In the main code editor window, right-click the
name
variable and choose Add Watch.The Watch window opens at the bottom of the code editor. You can use a Watch window to specify a variable (or an expression) that you want to keep an eye on.
Now, you have a watch set on the
name
variable, and you can see its value change as you move through the debugger. Unlike the other variable windows, the Watch window always shows the variables that you are watching (they're grayed out when out of scope).
Examine the call stack
While paused in the
for
loop, click the Call Stack window, which is by default open in the lower right pane.If it is closed, open it while paused in the debugger by choosing Debug > Windows > Call Stack.
Click F11 a few times until you see the debugger pause in the
SendMessage
method. Look at the Call Stack window.The Call Stack window shows the order in which methods and functions are getting called. The top line shows the current function (the
SendMessage
method in this app). The second line shows thatSendMessage
was called from themain
method, and so on.Note
The Call Stack window is similar to the Debug perspective in some IDEs like Eclipse.
The call stack is a good way to examine and understand the execution flow of an app.
You can double-click a line of code to go look at that source code and that also changes the current scope being inspected by the debugger. This action does not advance the debugger.
You can also use right-click menus from the Call Stack window to do other things. For example, you can insert breakpoints into specified functions, advance the debugger using Run to Cursor, and go examine source code. For more information, see How to: Examine the Call Stack.
Change the execution flow
Press F11 twice to run the
std::wcout
function.With the debugger paused in the
SendMessage
method call, use the mouse to grab the yellow arrow (the execution pointer) on the left and move the yellow arrow up one line, back tostd::wcout
.Press F11.
The debugger reruns the
std::wcout
function (you see this in the console window output).By changing the execution flow, you can do things like test different code execution paths or rerun code without restarting the debugger.
Warning
Often you need to be careful with this feature, and you see a warning in the tooltip. You may see other warnings, too. Moving the pointer cannot revert your application to an earlier app state.
Press F5 to continue running the app.
Congratulations on completing this tutorial!
Next steps
Run C++ In Visual Studio Code
In this tutorial, you've learned how to start the debugger, step through code, and inspect variables. You may want to get a high-level look at debugger features along with links to more information.
-->If you haven't downloaded and installed Visual Studio and the Microsoft C/C++ tools yet, here's how to get started.
Visual Studio 2019 Installation
Welcome to Visual Studio 2019! In this version, it's easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact.
Note
This topic applies to installation of Visual Studio on Windows. Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support .NET languages and cross-platform development. For installation instructions, see Install Visual Studio for Mac.
Want to know more about what else is new in this version? See the Visual Studio release notes.
Ready to install? We'll walk you through it, step-by-step.
Step 1 - Make sure your computer is ready for Visual Studio
Before you begin installing Visual Studio:
Check the system requirements. These requirements help you know whether your computer supports Visual Studio 2019.
Apply the latest Windows updates. These updates ensure that your computer has both the latest security updates and the required system components for Visual Studio.
Reboot. The reboot ensures that any pending installs or updates don't hinder the Visual Studio install.
Free up space. Remove unneeded files and applications from your %SystemDrive% by, for example, running the Disk Cleanup app.
For questions about running previous versions of Visual Studio side by side with Visual Studio 2019, see the Visual Studio 2019 Platform Targeting and Compatibility page.
Step 2 - Download Visual Studio
Next, download the Visual Studio bootstrapper file. To do so, choose the following button to go to the Visual Studio download page. Select the edition of Visual Studio that you want and choose the Free trial or Free download button.
Step 3 - Install the Visual Studio installer
Run the bootstrapper file you downloaded to install the Visual Studio Installer. This new lightweight installer includes everything you need to both install and customize Visual Studio.
From your Downloads folder, double-click the bootstrapper that matches or is similar to one of the following files:
- vs_community.exe for Visual Studio Community
- vs_professional.exe for Visual Studio Professional
- vs_enterprise.exe for Visual Studio Enterprise
If you receive a User Account Control notice, choose Yes to allow the bootstrapper to run.
We'll ask you to acknowledge the Microsoft License Terms and the Microsoft Privacy Statement. Choose Continue.
Step 4 - Choose workloads
After the installer is installed, you can use it to customize your installation by selecting the workloads, or feature sets, that you want. Here's how.
Find the workload you want in the Installing Visual Studio screen.
For core C and C++ support, choose the 'Desktop development with C++' workload. It comes with the default core editor, which includes basic code editing support for over 20 languages, the ability to open and edit code from any folder without requiring a project, and integrated source code control.
Additional workloads support other kinds of development. For example, choose the 'Universal Windows Platform development' workload to create apps that use the Windows Runtime for the Microsoft Store. Choose 'Game development with C++' to create games that use DirectX, Unreal, and Cocos2d. Choose 'Linux development with C++' to target Linux platforms, including IoT development.
The Installation details pane lists the included and optional components installed by each workload. You can select or deselect optional components in this list. For example, to support development by using the Visual Studio 2017 or 2015 compiler toolsets, choose the MSVC v141 or MSVC v140 optional components. You can add support for MFC, the experimental Modules language extension, IncrediBuild, and more.
After you choose the workload(s) and optional components you want, choose Install.
Next, status screens appear that show the progress of your Visual Studio installation.
Tip
At any time after installation, you can install workloads or components that you didn't install initially. If you have Visual Studio open, go to Tools > Get Tools and Features... which opens the Visual Studio Installer. Or, open Visual Studio Installer from the Start menu. From there, you can choose the workloads or components that you wish to install. Then, choose Modify.
Step 5 - Choose individual components (Optional)
If you don't want to use the Workloads feature to customize your Visual Studio installation, or you want to add more components than a workload installs, you can do so by installing or adding individual components from the Individual components tab. Choose what you want, and then follow the prompts.
Step 6 - Install language packs (Optional)
By default, the installer program tries to match the language of the operating system when it runs for the first time. To install Visual Studio in a language of your choosing, choose the Language packs tab from the Visual Studio Installer, and then follow the prompts.
Change the installer language from the command line
Another way that you can change the default language is by running the installer from the command line. For example, you can force the installer to run in English by using the following command: vs_installer.exe --locale en-US
. The installer will remember this setting when it's run the next time. The installer supports the following language tokens: zh-cn, zh-tw, cs-cz, en-us, es-es, fr-fr, de-de, it-it, ja-jp, ko-kr, pl-pl, pt-br, ru-ru, and tr-tr.
Step 7 - Change the installation location (Optional)
You can reduce the installation footprint of Visual Studio on your system drive. You can choose to move the download cache, shared components, SDKs, and tools to different drives, and keep Visual Studio on the drive that runs it the fastest.
Important
You can select a different drive only when you first install Visual Studio. If you've already installed it and want to change drives, you must uninstall Visual Studio and then reinstall it.
Step 8 - Start developing
After Visual Studio installation is complete, choose the Launch button to get started developing with Visual Studio.
On the start window, choose Create a new project.
In the search box, enter the type of app you want to create to see a list of available templates. The list of templates depends on the workload(s) that you chose during installation. To see different templates, choose different workloads.
You can also filter your search for a specific programming language by using the Language drop-down list. You can filter by using the Platform list and the Project type list, too.
Visual Studio opens your new project, and you're ready to code!
Visual Studio 2017 Installation
In Visual Studio 2017, it's easy to choose and install just the features you need. And because of its reduced minimum footprint, it installs quickly and with less system impact.
Prerequisites
A broadband internet connection. The Visual Studio installer can download several gigabytes of data.
A computer that runs Microsoft Windows 7 or later versions. We recommend Windows 10 for the best development experience. Make sure that the latest updates are applied to your system before you install Visual Studio.
Enough free disk space. Visual Studio requires at least 7 GB of disk space, and can take 50 GB or more if many common options are installed. We recommend you install it on your C: drive.
For details on the disk space and operating system requirements, see Visual Studio Product Family System Requirements. The installer reports how much disk space is required for the options you select.
Download and install
To download the latest Visual Studio 2017 installer for Windows, go to the Microsoft Visual Studio Older downloads page. Expand the 2017 section, and choose the Download button.
Tip
The Community edition is for individual developers, classroom learning, academic research, and open source development. For other uses, install Visual Studio 2017 Professional or Visual Studio 2017 Enterprise.
Find the installer file you downloaded and run it. The downloaded file may be displayed in your browser, or you may find it in your Downloads folder. The installer needs Administrator privileges to run. You may see a User Account Control dialog asking you to give permission to let the installer make changes to your system; choose Yes. If you're having trouble, find the downloaded file in File Explorer, right-click on the installer icon, and choose Run as Administrator from the context menu.
The installer presents you with a list of workloads, which are groups of related options for specific development areas. Support for C++ is now part of optional workloads that aren't installed by default.
For C and C++, select the Desktop development with C++ workload and then choose Install.
When the installation completes, choose the Launch button to start Visual Studio.
The first time you run Visual Studio, you're asked to sign in with a Microsoft Account. If you don't have one, you can create one for free. You must also choose a theme. Don't worry, you can change it later if you want to.
It may take Visual Studio several minutes to get ready for use the first time you run it. Here's what it looks like in a quick time-lapse:
Visual Studio starts much faster when you run it again.
When Visual Studio opens, check to see if the flag icon in the title bar is highlighted:
If it's highlighted, select it to open the Notifications window. If there are any updates available for Visual Studio, we recommend you install them now. Once the installation is complete, restart Visual Studio.
Visual Studio 2015 Installation
To install Visual Studio 2015, go to the Microsoft Visual Studio Older downloads page. Expand the 2015 section, and choose the Download button. Run the downloaded setup program and choose Custom installation and then choose the C++ component. To add C and C++ support to an existing Visual Studio 2015 installation, click on the Windows Start button and type Add Remove Programs. Open the program from the results list and then find your Visual Studio 2015 installation in the list of installed programs. Double-click it, then choose Modify and select the Visual C++ components to install.
In general, we highly recommend that you use the latest version of Visual Studio even if you need to compile your code using the Visual Studio 2015 compiler. For more information, see Use native multi-targeting in Visual Studio to build old projects.
When Visual Studio is running, you're ready to continue to the next step.