![How to Run C/C++ in Visual Studio Code How to Run C/C++ in Visual Studio Code](/images/thumbnail/How-to-Run-C-C-plus-plus-Code-in-visual-studio-code.png)
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 Code::Blocks for windows download](/images/screenshot/codeblocks-with-mingw-download-windows.png)
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 MinGW folder location in file explorer windows 10](/images/screenshot/copy-mingw-path-windows.png)
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 Edit the system environment variables windows search bar](/images/screenshot/edit-system-environment-variables.png)
Then go to the 'Environment Variables', select 'path', and click on the 'edit'
![Environment variables set windows 10 Environment variables set windows 10](/images/screenshot/environment-variable-setup.png)
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 Environment variables path setup for GCC windows 10](/images/screenshot/mingw-environment-variable-setup.png)
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 GCC version check in CMD using gcc --version command](/images/screenshot/gcc --version.png)
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 Code Runner Extension Visual studio code](/images/screenshot/code-runner-vs-code-extension.png)
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 C/C++ extension pack by Microsoft extension Visual studio code](/images/screenshot/c-c-plus-plus-extension-pack-by-microsoft.png)
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 PowerShell by Microsoft extension Visual studio code](/images/screenshot/powershell-extension-vs-code.png)
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 Drag and drop that file into the visual studio code](/images/screenshot/drag-and-drop-file-in-vs-code.png)
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 create a new c/c++ file in visual studio code](/images/screenshot/create-c-c-plus-plus-file-in-vs-code.png)
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) C++ code successfully running on visual studio code (vs code)](/images/screenshot/c-plus-plus-code-run-vs-code.png)
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.