How to Run C/C++ in Visual Studio Code

VS Code is an excellent tool for writing code. This is the most popular code editor used by programmers worldwide. From HTML to Blockchain smart contracts, every type of code can be written on this code editor. If you are looking for C/C++ run-on VS code, this article will significantly help you. After reading this article, you can run your C/C++ code on Visual Studio Code without a problem.


How to Run C/C++ in Visual Studio Code

Tutorial overview

We will need a compiler and some extensions to write c++ code and run it using the VS code. Here we will use the GCC compiler with the help of Code::blocks. So after following this tutorial, you can run your C/C++ code on both VS code and Code::Blocks. After successfully installing the GCC, we will install some necessary extensions that will help us to run the C/C++ code, and it will help us to write optimized code. Here are the steps you have to follow:

Install Proper Code::Blocks to Get GCC Compiler

We have to install Code::Blocks with the MinGW. To do that, go to Code::Blocks download, then download the "codeblocks- (Current version number) mingw-setup.exe."

Code::Blocks for windows download

After downloading, you have to install it. It is better to install it in the system default drive. Check everything when you are installing Code::Blocks. After installing, you can run a Hello World program to check if it's working or not.

Environment Variable Setup

You must set the environment variable to access the MinGW from your system drives. Before doing this, you should check if it already exists or not. To do that, open your command prompt and run the command 'gcc --version', and it gives any version-related info that means it's already there. Otherwise, you have to set the GCC accessible from anywhere. Here are the steps you have to follow:

  • First, go to the Program Files (of the drive where you installed the Code::Blocks, in default, you will find it in the C drive)
  • Go to CodeBlocks> MinGW> bin
  • Copy the path
MinGW folder location in file explorer windows 10

After copying, the path will look like that "C:\Program Files\CodeBlocks\MinGW\bin". To set that path as an environment variable, search 'Edit the system environment variables' in the windows search bar.

Edit the system environment variables windows search bar

Then go to the 'Environment Variables', select 'path', and click on the 'edit'

Environment variables set windows 10

Now you have to set the path you copied from the file explorer previously. Then hit save and ok.

Environment variables path setup for GCC windows 10

Then you can open the CMD and run the 'gcc --version' command to check if the GCC is working correctly.

GCC version check in CMD using gcc --version command

If it shows the version number, then your 50% work is done. Now you can move to the next step.

Install Required Extention for C/C++ in VS Code

We will need some extensions to run the C or C++ code on VS code. They will help us debug, build, highlight code, and many more. The first extension we need is Code Runner.

Code Runner Extension Visual studio code

This extension helps us to run/work with C, C++, Java, JS, PHP, Python, Perl, Ruby, Go, Lua, Groovy, PowerShell, CMD, BASH, F#, C#, and many more. The next extension we need is C/C++ Extension Pack by Microsoft.

C/C++ extension pack by Microsoft extension Visual studio code

This extension will install the necessary extension to run a c or c++ program. You don't have to think about the extensions for the c program.

For a better command line interface experience, you can use Powershell. This will provide you an excellent command line interface. You can access it from the terminal section of vs code.

PowerShell by Microsoft extension Visual studio code

That was some necessary extension for visual studio code to start with c or c++ in vs code. Many extensions will give you an excellent coding experience. For example, you can test themes for a good-looking interface.

Run Your First C/C++ Code on Visual Studio Code

After completing the steps above, your system can run the C/C++ code using VS code. Here are 3 steps you can follow to run C/C++ code on VS code:

Step1: Create a folder in any drive. Drag and drop that file into the visual studio code.

Drag and drop that file into the visual studio code

Step 2: Create files inside the folders using the proper C/C++ file name guideline with the proper file extension. Use '.c' for C program and '.cpp' for C++ program.

create a new c/c++ file in visual studio code

Step 3: Write a program using the run icon (top right side). First, it will build the project then it will run that program. You will see the build status in the terminal section and outputs in the output section of the terminal.

C++ code successfully running on visual studio code (vs code)

That's how you can run a C/C++ code in the visual studio code. If you face any problems related to this, you can comment below. I will try to solve that for you.

Author: Sakib Mahmud

This article is from Sakib Mahmud.
(Computer science student from Bangladesh)


  • Published: 2 Dec 2022
  • Last Update: 3 Dec 2022

⚫ More Article On C and C++: