If you struggle to correctly install C in Windows PC with Visual Studio Code and MinGW, then your struggle ends here. In this article, I’ll guide you on how to properly install and configure C in Windows with Visual Studio Code and MinGW.
Follow the steps given below to install and configure C in Windows:
Step 1: Installing VS Code
The first step is installing Microsoft Virtual Studio Code on your Windows. Well, one can use other code editors too, like Sublime Text Editor, but I would recommend you install Microsoft’s Visual Studio Code. To install VS code, follow the steps given below :
- Click here to visit the download page of Visual Studio Code.
- After that, click on the Windows button to start downloading VS Code for Windows.
- You’ll see thank you message, and your visual studio code will start downloading.
- After the Visual Studio Code setup is downloaded, run the setup and accept the agreement. After that, click on the next button.
- Select the destination where you want to install Visual Studio Code and then click on the Next button.
- Select the Start Menu folder and then click on the Next button. I would recommend you to leave it as it is and then click on the Next button.
- Advertisement -
- In the Select additional tasks section, make sure to check all the boxes and then click on the Next button.
- Now Visual Studio Code is ready to get installed, so click on the Install button to start installing Microsoft Visual Studio Code.
- The installation will take some time, and after it is completed, click on the finish button to exit the VS code setup.
Now that we’ve installed Microsoft Visual Studio code to write our C programs. Let’s install the MinGW compiler for our C Programs.
Step 2: Downloading and setting up MinGW For C in Windows
In this step, we are going to download MinGW and set it up so that we can compile our C programs in windows without any errors.
- Click here to visit the download page of MinGW.
- Download the latest version of MinGW from the download section.
- After the download is completed, run the MinGW setup and then extract it in Local Disk C. Anyway, you can extract it anywhere but I would recommend you extract it in Local Disk C.
- After the extraction is completed, you’ll see the MinGW folder at the place you extracted it. In my case, it is in Local Disk C cause I extracted the MinGW setup in Local Disk C.
- Advertisement -
Now that we’ve installed MinGW, let’s set up the environment variable path for MinGW in windows so that we can compile and run C/C++ programs in Windows.
Setting Up Environment Variable For C In Windows
- To set up the environment variable path for MinGW, get inside of bin folder that is in the MinGW folder.
- Copy the path of this folder from the address bar of file explorer.
- Now right-click on This PC and then click on Properties.
- Now from properties click on Advanced system settings.
- From Advanced system settings, click on Environment Variables.
- Now in Environment Variables, select Path and then click on Edit.
- Here click on the New button to create a new path and then paste the path you’ve copied and then click on the OK button to save it.
We’ve successfully set up MinGW and its path for environment variables so that we can use GCC to compile and run C in Windows PC.
Checking MinGW Installation
To confirm if MinGW is installed or not properly on your Windows PC. Type the gcc command in your windows terminal. If you see a fatal error message, as shown in the image below, that means you’ve installed MinGW in your Windows properly.
You can also check your GCC version using the command
gcc --version in your terminal.
Installing C/C++ IntelliSense In VS Code
Now we are ready to write and run C in Windows PC, but before that, I would like to tell you about a VS code extension that will help you write C code.
C/C++ Intellisense is a VS Code extension that helps write C/C++ code faster. So to install it get in the VS code extensions section, search for “C/C++ IntelliSense and then Click on the Install button to install it.
Running C/C++ Program
Now that we’ve set up everything required to write and run C/C++ in windows, let’s run a simple hello.c program in our windows.
To run a C program, we need to compile it using GCC. So to compile your C code, type gcc and then the file name of your code in the terminal and hit enter. In my case, to compile hello.c code I need to type
gcc hello.c in my terminal.
gcc <file-name> #To compile code gcc hello.c
It will take a few seconds to compile after it is done, you’ll see a new file with the .exe extension. In my case, it was a.exe, this exe file is your compiled code. Now to run the code type ./ and the file name in the terminal. So In my case, I need to type ./a.exe to run my code, and then I’ll see the results.
So this is how you can set up C in Windows PC to write and run C code. If you are stuck somewhere, whether in setting up MinGW or running C code watch my YouTube video given below to understand it.