How to run a C or C++ program in VS Code

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

Step 1: Download and install C or C++ compiler

To start writing C/C++ code in VS Code, you need to install a C and C++ compiler. The compiler you use depends on your OS.

Using Windows, I will show you how to download and configure MinGW. You can skip until Step 3 if you are using Linux or Mac.

  1. Go to sourceforge.net/projects/mingw and download the MinGW setup.

    Download MinGW
    Download the MinGW setup.

  2. Run the setup file and select Install.

    Install MinGW
    Run the setup and select Install.

  3. Keep the default path for installation. In my case, it is C:MinGW. Select Continue.

    Install MinGW Step 1
    Keep the default path and select Continue.

  4. When the installation is complete, click on Quit.

    Install MinGW Step 2
    Click on Quit after installation completion.

  5. Launch the newly-installed MinGW application.
  6. Under the Basic Setup tab, select mingw32-GCC-g++.

    Basic Setup
    From the Basic Setup section, choose the mingw32-GCC-g++ option.

  7. Select Mark for Installation.

    Mark for Installation
    Select Mark for Installation.

  8. From the top-level Installation menu, select Apply Changes.

    Apply Changes
    In the Installation menu, select Apply Changes.

  9. Finally, click Apply.

    Apply
    Click on Apply.

Step 2: Configure MinGW

Before running your C or C++ program, you must add the MinGW bin directory to your system path. Follow these steps:

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

    From the start menu, search for “Path.” Then, select Edit the system environment variables option.

    Edit the sytem environment variables
    Search for “path” and select Edit the system environment variables.

  2. Select Environment Variables.

    Environment Variable
    Select Environment Variable.

  3. Select Path under the “Variable” column. Then, click on Edit…

    Path and Edit
    Select Path, then click on Edit.

  4. Paste the copied path here and select OK.

    Paste the path
    Paste the copied path here, then click OK.

Step 3: Run your C and C++ Program

  1. Launch VS Code. Create a new source file by clicking on the New File icon. Then, type the source file name with the .c extension. I will name it HelloWorld.cNote:Alternatively, you can select the folder+ icon and select the folder which contains your C or C++ project to import it.

    HelloWorld.c
    Create a new C file.

  2. Write, or paste your code into the editor. For this tutorial, I will use this code to print a simple “Hello World” 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 your code, click the Run button on the top right. Alternatively, you can press Ctrl + Alt + N.

    Run your code
    Press the Run button or Ctrl + Alt + N to run the program.

  5. Your code’s output will show in the terminal. You will also find a .exe file of your program in the “Explorer” view.

    Output
    The result of your program is shown in the terminal console.

Leave a Comment