Running a C program in Visual Studio Code (VS Code) is a straightforward process. VS Code is a lightweight and versatile code editor that supports various programming languages, including C. To run a C program in VS Code, you need to install the necessary extensions, configure the compiler, and use the integrated terminal to compile and execute your code. VS Code provides a seamless environment for writing, debugging, and running C programs.In this article, we will learn to use C programming in VS Code. We will see how to run a C program in Visual Studio Code.
What is Visual Studio (VS) code?
VS Code is an editor that helps to run code. Visual Studio is a free code editor used to write programming codes, helps in solving bugs, and corrects mistakes in the code using the intelli-sense method.
It provides users to write code in a very easy way. Most programmers say that it is an IDE as well as an editor, but the final decision is up to the programmer. Earlier, the coding was done on old editors or even in basic editors such as notepad++ or sublime text. These editors only provide basic help to the programmers.
Prerequisites for running a C program in VS Code
To run a C program in Visual Studio Code (VS Code), you need to have the following prerequisites:
C Compiler: You need a C compiler to compile and run your C programs. Some famous c compilers are:
GCC on Linux
GCC via Mingw-w64 on Windows
Microsoft C++ compiler on windows
Clang for XCode on MacOS
C/C++ Extension for Visual Studio Code: Search for "C/C++" in the Extensions and click on the extension provided by Microsoft. Click the "Install" button to install the extension.
You should also have basic knowledge about C language to code and run c programs.
How to Setup C Programming in VS code?
Now, we know what VS code is. Here, we will learn how to set up a C programming in VS code step by step. But for this, there are a few things that we need to know:
Basic knowledge of C programming
Installing Visual Studio Code
Step 1 - Setup & Install the C Extension
After Installing the VS code, the starting screen will look like this:
1. First of all, we need to click on the extension button, as shown in the image below, that displays a sidebar to download and install the C/C++ extension in VS code. In the sidebar, you need to type C Extension.
2. After that, we need to click on the C/C++ option, as shown in the image below.
Now, you need to click on the Install button that will install the C extension.
3. After clicking on the Install button, it will install the C/C++ Extension.
Step 2 - Setup and Install Compiler Extension
A MinGW is a GCC compiler used to compile and execute code. It is advanced software that supports only the window OS.
2. Now, you need to click the Download button, as shown in the image, and it will start downloading the compiler.
3. Now, the MinGW GCC compiler software has been downloaded successfully.
4. After that, we have to double-click on the MinGW downloaded file to run the installation.
5. Now, click on the run button and then Click on the Install option.
6. After that, you need to set the install location, or you can keep it as default. Now, you need to click on the Continue option.
7. After we click on Continue, the next screen will be the MinGW Installation Manager. In this, we have to check the Ming32-GCC-g++ package and the Mingw32-base package to run and compile the program in the VS code.
8. After that, you need to select that checkbox and click on the Installation tab. Then we need to click on the Apply Changes option to set the package's installation.
9. When you click on the Apply button, it will show the screen as shown in the below image.
10. After downloading the packages, it will show the installation processes of the packages, and then you need to click on the Close button when all the installations are done.
Step 4 - Set the Environment Path for the MinGW
After installing the MinGW GCC compiler, we need to set up the environment path to include the C/C++ compiler directory.
1. First, you need to go to the installation directory of the MinGW Set Up.
2. Now, you have to double-click on the MinGW folder.
3. Then, click on the bin folder, and there you need to copy the directory path.
4. Now, you need to go to This PC, then Right Click on This PC, and Click on the properties option.
5. Now, click on the Advanced system settings option that will display a popup box of the System Properties.
6. After that, Click on the Environment Variables option to set up the directory path as shown in the above image.
7. First of all, you need to click on the System Variables Path, and then you need to click on the Edit button.
8. After clicking the edit button, it will show a popup window to set up a new path.
9. Then, we have to click on the New button and paste the C:\MinGW\bin path. Then, click on the OK button.
10. Now, you need to click on the OK button to the Environment Variables also and then System Properties.
Step 5 - Start Coding in the Visual Studio Code Editor
1. Now, you need to create a folder to store all program codes. We can create a folder with any name in any directory. For example, we are writing a C program, and then we will create e a folder with the name “C Program”.
2. Now, we need to open the Visual Studio Code and click on the Open Folder option.
3. After clicking on the Open Folder option, a popup dialog box will be displayed, and then we need to select our folder to store the program. In this case, our folder is “C Program” and then click on the Add option.
4. Now, move the cursor over the C Program folder. It will show a “+” and you need to click on the button and then write the file name as Codingninjas.c.
Now, we will add a simple C program in the VS Code editor.
C
C
#include<stdio.h> void main() { printf("Welcome to Coding Ninjas"); /* print the statement */. }
Now, when we complete the code, we need to save the file. After saving the file click on the icon as shown below and click on the Run C/C++ file option. Once this is done the in-built terminal of VS code will appear and the file will start compiling. And once compilation is completed an executable file of the same name with .exe file extension will appear in the explorer and output will be shown in the terminal.
To run a C program, first write your code in a .c file. Then use a C compiler like GCC to compile it with a command like gcc -o program program.c. Finally, run the compiled executable with ./program in a terminal.
How to create C file in Visual Studio?
Launch Visual Studio → Open any folder → Select project template → Select C++ or C# Console App → Add a C file and give (.cpp) file extension to code in C++ or (.cs) for C# → Start Coding.
How to run C code?
To run C code, you need to compile it into an executable file and then execute that file. Follow the below steps. Write your C code → Save the file with “.c” extension → Open a terminal or command prompt → Navigate to directory with you file. → compile the C code → Run the executable.
How to compile C language in VS Code?
Use keys Cmd + Shift + B to compile and run the C language codes in VSCode.
Conclusion
In this article, we talked about how to run a C program in Visual Studio Code. We covered setting up the C/C++ extension, configuring the compiler, and creating tasks to build and run the program directly within the IDE. This streamlined approach simplifies coding and debugging C programs, enhancing productivity and learning. We learned what VS code is and how to run a C Program in Visual Studio Code.
You can also refer to other similar articles that will help you in programming -