How To Run C and C++ Code In Visual Studio Code

C and C++ are widely used general-purpose programming languages. This article will teach you how to run C and C++ Code In Visual Studio Code.

Download And Install MinGW

To start writing C/C++ code, you need to install C and C++ toolsets, i.e., MinGW. Follow these steps:

  1. Head over to sourceforge.net/projects/mingw and click on the Download button.

    Download MinGW
    Follow the link and download the MinGW setup.
  2. Run the downloaded setup file and select Install.

    Install MinGW
    Click on the Install button.
  3. Keep the default path for installation. In my case, it is C:\MinGW. Select Continue.

    Install MinGW Step 1
    Run the download and keep the default path for installing.
  4. Click on Quit.

    Install MinGW Step 2
    Click on the Quit button.
  5. Launch the now-downloaded MinGW app.
  6. Under the Basic Setup tab, select the mingw32-GCC-g++ option.

    Basic Setup
    From the Basic Setup section, choose the mingw32-GCC-g++ option.
  7. Select Mark for Installation.

    Mark for Installation
    Click on the Mark for Installation option.
  8. From the top-level “Installation” menu, select Apply Changes.

    Apply Changes
    Under the Installation menu, select the Apply Changes option.
  9. Then, select Apply.

    Apply
    Click on the Apply button.

Configure MinGW

To configure MinGW, you must add the MinGW bin directory to your system path. Follow these steps:

  1. Copy the path of the bin folder inside your MinGW folder. By default, it is C:\MinGW\bin.
    Bin Path
    Copy the path of the bin folder inside of the MinGW folder.

    From the start menu, search for “Path.” When prompted with options, choose the Environment Variables option.

    Edit the sytem environment variables
    Look for “path” in the Start menu, and open the first option that appears.
  2. Select Environment Variables.

    Environment Variable
    Select the Environment Variable option.
  3. Select path under “variables” column. Then, click on Edit…

    Path and Edit
    Select the Path option and then click on Edit.
  4. Paste the copied path here and select OK.

    Paste the path
    Paste the copied path here and then click on the Ok button.

How To Run C and C++ Code In Visual Studio Code?

  1. In VS Code, create a source file by clicking on the New File icon. Then, type the source file name with the .c extension. I will name it HelloWorld.c

    HelloWorld.c
    Create a new C file.
  2. Write your code inside the editor. For this tutorial, I will use this code to print a message:
    #include <stdio.h> int main(void) { printf("Hello World!")return 0;}

    HelloWorld Code
    Write your code.
  3. Press Ctrl + S on your keyboard to save the file.
  4. To execute the code, click on the Run button on the top-right corner of the editor. Alternatively, you can press Ctrl + Alt + N.

    Run your code
    Press the Run button or press Ctrl + Alt + N to run the program.
  5. It will bring up the output in the terminal. You will also find a .exe file of your program in the “Explorer” view.

    Output
    The result will be presented in the terminal console.

Was this article helpful?

Leave a Comment