How to setup C/C++ in Visual Studio Code?

Are you trying to learn C or C++ programming using Visual Studio Code, or are you experiencing issues during the installation process?

In this guide, we will walk you through the step-by-step process of installing the “MinGW” compiler and running C and C++ programs in Visual Studio Code.

Steps on how to set up C/C++ in Visual Studio Code:

1/ Download MinGW

The first step in setting up your C/C++ development environment is to download the MinGW (Minimalist GNU for Windows) compiler.

MinGW is a collection of free and open-source compilers for C and C++ languages.

  1. Go to the MinGW download page at https://sourceforge.net/projects/mingw/.
  2. Download the MinGW installation executable.

download mingw form sourceforge site

2/ Install MinGW

After downloading the MinGW installer, follow these steps to install it:

  1. Run the MinGW installer executable.
  2. The setup screen will appear. Click on the “Install” button. mingw setup screen
  3. On the next setup screen, select the path (e.g., C:\MinGW) where you want to install MinGW, and then click “Continue.” mingw setup screen path and continue
  4. The installation process will begin. Wait for it to complete, and then click “Continue.”

mingw installation process

3/ Configure MinGW Installation

Now that MinGW is installed, let’s configure it for C/C++ development:

  1. The MinGW Installation Manager will appear. Mark the checkboxes for “mingw32-base” and “mingw32-gcc-g++” for installation.

    mingw installation manager one

    mark mingw32 base and gcc

  2. Click on the “Install” option in the menu and then select “Apply Changes.”

    click on install and apply changes

    click apply again to proceed

  3. The installation process for the selected compilers will begin.

    installation process for the selected compilers

4/ Complete the Installation

After the installation of the selected packages is finished, follow these steps:

  1. Click “Close” to exit the MinGW Installation Manager.

    close after installation

    click on close button again

  2. Open the folder where MinGW is installed and locate the “bin” folder.

    open the mingw folder in windows

    open the bin folder in the mingw folder

  3. Copy the file path for the “bin” folder.

    copy file path for bin

5/ Configure Environment Variables

To ensure that Visual Studio Code can access MinGW, you need to add the MinGW “bin” folder path to your system’s PATH environment variable:

  1. In the Windows search bar, type “environment” and then click on “Edit the system environment variables.”

    click on environment variable

    click on edit environment variable

  2. In the System Properties window, click on the “Environment Variables” button.

    click on environment variable button on the system properties

  3. In the Environment Variables window, locate the “Path” section, and click “Edit.” click on path

  4. Click on “New” to add a new entry, and paste the file path for the “bin” folder you copied earlier.

    click on new and add the file path

  5. Click “OK” to save the changes. click ok after adding the file path

6/ Verify MinGW Installation

To verify that MinGW is correctly installed and configured, follow these steps:

  1. Open PowerShell or Command Prompt.

  2. Type the following command and press Enter:

    gcc --version
    

    This command will display the installed GCC version. type gcc version to check if mingw is installed correctly

7/ Create a C/C++ File in Visual Studio Code

Now that you have set up MinGW and configured your environment, it’s time to create your first C/C++ program in Visual Studio Code:

  1. Open Visual Studio Code.

  2. Create a new “C” file and add a simple “Hello, World!” code.

    create c file and simple c code

    #include <stdio.h>
    
    int main(){
    
        printf("Hello world!");
    
        return 0;
    }
    

8/ Install the Code Runner Extension

For an even smoother coding experience, consider installing the “Code Runner” extension in Visual Studio Code:

  1. Go to the Visual Studio Code extensions marketplace.

  2. Search for “Code Runner” and click “Install.”

    create c file and install code runner extension

  3. After successful installation, open your C file and click on the “Run” button above it.

code runner installed

run the code

Troubleshooting ⚠️

If you encounter any issues while running your code and the output is not as expected, try restarting VSCode.

Also, if the code runs but is not showing any output, you can use the terminal within Visual Studio Code:

  1. In the terminal, type the following command, replacing <file-name-with-extension> and <file-name-without-extension> with your file names:

    gcc <file-name-with-extension> -o <file-name-without-extension>
    
  2. Click enter then, type this and then click enter again:

    ./<file-name-without-extension>
    

Here’s an example:

gcc app.c -o app
./app

“app.c” is the filename with .c extension.

if the ouput keeps running with no output

Here is an Installation Video for More Guide: