How To Run A C Program In Visual Studio Code

  • We should have a basic knowledge of C programming.
  • The Visual Studio Code Editor must be installed in the system.
  • Download the C/C++ Extension. It is an extension provided by Microsoft that support visual studio code. It helps in IntelliSence, debugging and code browsing of the programming code in the visual studio.
  • Download the C/C++ compilers. There are some popular compilers are: GCC on Linux GCC via Mingw-w64 on Windows Microsoft C++ compiler on windows Clang for XCode on MacOS
  • GCC on Linux
  • GCC via Mingw-w64 on Windows
  • Microsoft C++ compiler on windows
  • Clang for XCode on MacOS

We have successfully set up Visual Studio Code on our system. The interface of VS Code appears as shown below:

Download & Install the C/C++ Extension

  1. We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension .
  2. After that, click on the C/C++

In this visual representation, select the Install option to add the C/C++ extension.

  1. Upon clicking the Install button, the following image will be displayed.

In this illustration, the presence of the Uninstall button indicates that the C/C++ extension has been effectively installed in Visual Studio Code.

In this illustration, we observe the presence of the Uninstall button, indicating the successful installation of the C/C++ extension within Visual Studio Code.

Download and Install Compiler Extension

MinGW is a sophisticated compiler based on GCC, employed for compiling and running code. It specifically caters to the Windows operating system exclusively.

Download the MinGW-w64 Compiler

  1. Go to the https://sourceforge.net/projects/mingw We land on the following page.
  2. After that, click on the Download button, then it starts the downloading of the MinGW GCC compiler , as we can see in the below image.
  3. MinGW software has been successfully downloaded into the system.
  4. Now we double-click on the MinGW set up to install the compiler.

As we can see, it shows that it is a harmful file click on the Run button to proceed with installing the setup.

  1. Click on the Install
  2. Set it defaults, or we can change the storage location of the setup. After that, click on the Continue
  3. After clicking the continue button, it shows step 2 of MinGW Installation Manager.
  4. As we click on the Continue , it shows the below image. In the MinGW Installation Manager , we need to check the Mingw32-base package and Ming32-gcc-g++ package to run and compile the C/ C++ program in the visual studio code editor.
  5. After selecting the checkbox, click on the Installation tab (at the top left corner of the dialog box).

Here we select Apply Changes to configure the installation of the package in MinGW, as illustrated below.

Upon clicking the Apply button, the following image is displayed.

Upon completing the package download, the installation progress of the package is shown as depicted below.

Here we can observe that all modifications have been effectively implemented before clicking on the Close button.

Set the Environment Path for the MinGW Set Up

After downloading and installing the MinGW compiler, we now set the environment path to include the C/C++ compiler directory.

  1. Go to the installation directory of the MinGW Set Up. Here we installed the setup at the C drive, as shown below.
  2. Double click on the MinGW folder. It shows the below image.
  3. After that, click on the bin folder and then copy the directory path, as shown below.

Here is the path of the MinGW folder path: C:\MinGW\bin

  1. After copying the directory path, go to This PC -> Right Click on This PC -> Select/ Click on the Properties . It shows the below image.
  2. After that, click on the Advanced system settings to display a popup box of System Properties, as shown below.
  3. Click on the Environment Variables to set the directory path, as shown below.

First, we need to select the System Variables Path and then proceed to select the Edit button, as demonstrated in the image above.

Upon clicking the Edit button, a popup window will appear, allowing us to define a new path, as depicted below.

In the image above, we initiate the process by selecting the New option and then inserting the path C:\MinGW\bin; subsequently, proceed by clicking the OK button.

To confirm the successful installation of MinGW on the system, repeat the action by clicking the OK button within the Environment Variables and System Properties.

For verification of the MinGW installation, navigate to the Command Prompt (cmd), input gcc -version, and then hit Enter.

Start Coding in the Visual Studio Code Editor

  1. Here we created a C Program folder to store all program code. We can create a folder with any name in any directory.
  2. Go to the VS Code and click on the Add Folder .
  3. As we click on the Add Folder, it shows a popup dialog box to select the folder to store the program.
  4. After selecting the folder, click on the Add The selected folder appears in the explorer section, as we have shown below.
  5. Move the mouse over the C PROGRAM folder; it shows a + Click on the button and write the file name as Logic Practice.c , as shown below.

Now learn and comprehend basic C programming within the VS Code text editor.

Logic Practice.c

Example

#include<stdio.h> // define the header file
void main()   // define the main function
{
    printf("Welcome to Logic Practice");  // print the statement.
}

After completing the code, simply click with the right mouse button on the program, following the steps illustrated below.

Select the "Run Code" option or use the key combination Ctrl + Alt + N to execute the code. This action will display the output as shown below.

Example

Welcome to Logic Practice

Let's develop a script in the VS Code text editor to compute the area and perimeter of a rectangle.

Rectangle.c

Example

#include<stdio.h> // header files
#include<conio.h>
void main()
{   // initialize the local variables.
    int l =5, b=10, ar, pr;
    printf("Length & Breadth of the rectangle is: %d & %d",l, b);
    ar = l * b; // calculate area of rectangle.
    pr = 2 * (l + b); // calculate perimeter of rectangle.
    printf("\n Area of Rectangle is: %d", ar);
    printf("\n Perimeter of Rectangle is: %d", pr);  
}

We have the option to either click on the Execute button or use the shortcut Ctrl + Alt + N on the keyboard. This action will showcase the following result.

Example

Welcome to Logic Practice

Let's create another C program to receive user input within the Visual Studio Code IDE.

Rectangle2.c

Example

#include<stdio.h>
int main()
{   // initialize the local variables.
    int l, b, ar, pr;
    printf("Enter the length of the rectangle");
    scanf("%d", l); // take input from the user
    printf("Enter the breadth of the rectangle");
    scanf("%d", b); 
    ar = l * b; // calculate the area of rectangle.
    pr = 2 * (l + b); // calculate the perimeter of rectangle.
    printf("\n Area of Rectangle is: %d", ar);
    printf("\n Perimeter of Rectangle is: %d", pr);  
}

Upon clicking the Execute button or utilizing the Ctrl + Alt + N shortcut, the following result is presented.

In the program mentioned above, the user inputs the dimensions of length and width using the keyboard. Upon compilation, the program generates the following output.

Add the following steps in the code editor to allow user input from the console:

  1. Create a variable to store the user input.
  2. Use a function like input to prompt the user for input.
  3. Assign the user input to the variable created.
  4. Utilize the user input in the code as required.

Following are the steps to take input from the user.

  • First of all, we need to stop the background running the c program by pressing the Alt + Ctrl + M from the keyboard.
  • After stopping the C file, go & click the File button at the top left corner of the Visual Studio Code Editor, and select the Settings via Preferences , as shown below image.
  • After clicking the Settings , it shows the image below. In this image, select the extension button to set the settings for the C Compiler.
  • Click on the Extension button and scroll the drop-down box to select the Run Code Configuration .
  • Now scroll the right-side pane and Tick on the Run In Terminal.
  • Go to the c and again execute the program by clicking on the Run , it produces the following results, as shown below.

Input Required

This code uses input(). Please provide values below: