Run C In Visual Studio Code



Gourav Goyal

How to run c in visual studio code. Cpp by Grieving Goosander on Mar 14 2021 Donate. How to run cpp using gcc vscode. Cpp by Yawning Yak on Dec 15 2020 Donate. 0 Source: stackoverflow.com. Add a Grepper Answer.

  1. Run in Terminal. Now our program will run in the TERMINAL tab and we will be able to enter data if we need to. And that’s it, following these steps you will be able to compile and run code in C/C using Visual Studio Code.
  2. VSC – C/C Extension download. Now your VS code is ready to compile and run C/C code but also to obtain output in one click, We need another extension. Here, We have picked Code Runner Extension. You can download this extension from the extension center or click here to install. VSC – Code Runner Extension Download.
  3. There is a much easier way to run Java, no configuration needed: Install the Code Runner Extension; Open your Java code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will be compiled and run, and the output will be shown in the Output Window.

By the end of this short guide, you’d be able to run, debug, and get IntelliSense for C/C++ files in VSCode. Though, this guide is focused on the Windows platform but can be extended to Mac and Linux with some minor changes.

I extensively used C & C++ in my competitive programmingyears and wanted better support for debugging & IntelliSense. The only options availablewere Dev-C++ (outdated) and the original 'Mammoth'Visual Studio. Lately, I found VSCode and fell in love with it (first love was Atom).I tweaked it around and set it up as a complete IDE For small C, C++ projects especiallygeared towards competitive programming.

Create a sample C/C++ project

Code
  1. Open/Create an empty folder in VSCode.
  2. Create a new.cpp file inside it like below:
new.cpp
  1. Install recommended C/C++ extension in VSCode and reload.

Install C/C++ Compiler

C/C++ extension does not include a C++ compiler. So, you will need to install one or use which is already installed on your computer.

Windows: Download MinGW64.zip (latest release) and extract it to the C Drive.

Mac:XCode

Visual Studio C Programming

Run

Linux:GCC

Also, Make sure to add C++ compiler PATH to environment variable of your platform. For Windows MinGW64 add: C:MinGW64bin

Run and Debug C/C++ Code

You’ll notice that there is also a .vscode folder in your sample project. To configure debug configuration, 2 files are required launch.json and tasks.json inside .vscode folder.

VSCode can create and auto-configure these files if we try to debug for the first time. To do that, open C++ file in VSCode and either hit F5 or go to Debug -> Start Debugging and select C++ (GDB/LLDB) then select g++.exe build and debug active file.

This should create 2 files launch.json and tasks.json in .vscode folder which should look like below (update the MinGW64 path if not correct)

Run C++ In Visual Studio Code

Notice that I’ve added one more optional configuration g++ build & run active file in launch.json and g++ build & run in tasks.json file for purpose of also Running C/C++ code without debugging. Now you may choose which configuration to pick when you start debugging. You may remove the configuration whichever you won’t need.

Compile C In Visual Studio

launch.json

tasks.json

Visual Studio Code C++ Compiler

tasks.json

externalConsole in launch.json can be set to true to see code output in cmd instead.

Restart VSCode to take effects of newly added compiler paths.

Open any C/C++ file, set some breakpoints (or not), and hit the Big Green Play Button.

(Shortcut to debug: F5 )

C Language In Visual Studio

Tip: To hide *.exe files in the side explorer of VSCode, open settings and paste the below config:

How To Run C Code In Visual Studio 2019

Thanks for reading. Would love to hear your thoughts about it. Connect with me onTwitter andLinkedIn.