

You have written your first C program, but clicking the Run button does nothing. Or perhaps VS Code displays an error saying that the compiler command is not recognised. If this sounds familiar, the problem may not be your code at all.
Visual Studio Code is a code editor, not a compiler. It provides a place to write and manage code, but you need a separate compiler to convert your C program into an executable file. Once you install and configure the compiler, running C code in VS Code becomes much easier. Here is how to set up VS Code, install MinGW on Windows, create your first C file and run it using the terminal.
Before writing your first program, make sure you have an editor, a compiler and the required system settings.
You need three things to run a C program in Visual Studio Code:
Visual Studio Code: The editor where you write and manage your C programs.
A C compiler: MinGW, which provides GCC, is one option for Windows users. It converts C source code into an executable program.
Compiler added to PATH: This allows Windows to recognise commands such as gcc from the terminal.
You can download VS Code without installing a compiler, but you will not be able to compile and execute C programs until a compiler is available.
MinGW provides the GCC compiler required to compile your C program on Windows.
Follow these steps to install it:
Search for the official MinGW download page and download the installer.
Open the downloaded installer and click Install.
Once the installation manager opens, select the packages required for C compilation, including the GCC compiler.
Apply the changes and allow the required files to download.
Wait for the installation to finish.
The compiler files are generally available inside the MinGW installation folder. You will need the location of its bin folder in the next step.
Confused about how GCC turns your .c file into an executable? Learn the complete C compilation process.
Install VS Code separately because the editor and compiler perform different jobs.
Visit the official Visual Studio Code website.
Download the Windows installer suitable for your system.
Open the installer and accept the licence agreement.
Follow the installation instructions. You can keep the default settings.
Select options such as adding VS Code to PATH, if available.
Complete the installation and launch VS Code.
Once the editor opens, you can configure it to work with the compiler installed earlier.
Adding the MinGW bin folder to PATH allows the terminal to find GCC without requiring its full location every time.
Here is how you can do it:
Open File Explorer and locate the MinGW installation folder.
Open its bin folder and copy the folder address from the address bar.
Search for Edit the system environment variables in Windows Search.
Open the option and click Environment Variables.
Under the available variables, select Path and click Edit.
Click New, paste the copied MinGW bin folder address and confirm the changes.
Click OK to close the open settings windows.
For example, your compiler path may look like C:\MinGW\bin, depending on where you installed MinGW.
Close and reopen VS Code after updating PATH so that its terminal can recognise the new setting.
Install the relevant extensions to make writing and running C code easier.
Open VS Code and select the Extensions icon from the sidebar. Search for and install:
C/C++ by Microsoft: Provides code completion, syntax support and error highlighting for C and C++.
Code Runner: Adds a convenient option to execute code from the editor.
You can also open Code Runner settings and enable Run In Terminal. This is particularly useful when your C program requires user input.
Save your settings and keep VS Code open for the next step.
Create a folder, open it in VS Code and save your program with the .c extension.
Follow these steps:
Create a folder on your computer, such as C Programs.
Open VS Code and select File > Open Folder.
Choose the folder you just created.
Click the New File icon and name the file hello.c.
Enter the following C program:
Press Ctrl + S to save the file.
The .c extension identifies the file as a C source file. The program uses printf() to display the message and return 0 to indicate that it has finished successfully.
Want to understand the code before moving ahead? Read PW Skills' C Programming Foundations course.
You can execute your program using the Code Runner button or compile and run it directly from the VS Code terminal.
If you have installed the Code Runner extension:
Open your hello.c file.
Save the program using Ctrl + S
Click Run Code at the top-right corner of the editor.
Check the output in the terminal or output panel, depending on your settings.
You should see:
You can also compile and execute your program manually. This method helps you understand what happens when a C file is converted into an executable.
Open the VS Code terminal using Ctrl + `
Enter the following command:
Press Enter to compile the program.
Run the generated executable using:
The first command uses GCC to compile hello.c and create an executable named hello.exe. The second command runs that executable.
If the terminal displays Hello, World!, your C program has run successfully.
Struggling to practise C after setting up VS Code? Watch this beginner-to-advanced C Language Full Course and solve 100+ questions step by step.
Mac users need a compiler toolchain for macOS instead of following the Windows MinGW PATH steps.
You can use Apple's Command Line Tools to install the compiler tools:
Open Terminal on your Mac.
Run the command:
Follow the installation instructions.
Download and install the macOS version of Visual Studio Code.
Install the C/C++ extension and create a file named hello.c.
Open the VS Code terminal and compile the file:
Run the program:
The terminal should display Hello, World!. The same approach works on Apple Silicon and Intel Macs once the required compiler tools are installed.
If you want to practise C concepts alongside your setup, you can also watch PW's C Language Full Course
Most beginner errors are caused by compiler installation, PATH settings or incorrect file names.
|
Error or issue |
What you can check |
|
gcc is not recognised |
Check whether the MinGW bin folder is added correctly to PATH. Restart VS Code after changing PATH. |
|
No such file or directory |
Check whether the terminal is open in the folder containing your .c file. |
|
undefined reference |
Review your code for missing function definitions or incorrect compilation commands. |
|
No output appears |
Save the file, check the terminal and confirm that the program contains a working output statement. |
|
Code Runner does not execute the program |
Check the extension settings and confirm that the compiler is installed and available. |
If the error continues, try compiling the file manually through the terminal. This can help you identify whether the issue is with the compiler setup or the editor extension.
VS Code is a code editor, so you need a separate C compiler such as MinGW on Windows.
Add the compiler's bin folder to the Windows PATH so the terminal can recognise GCC.
Install the C/C++ extension and, optionally, Code Runner to make coding easier.
Save your program with a .c extension before compiling it.
Use gcc hello.c -o hello.exe to compile a Windows C program and .\hello.exe to run it.
Mac users can install Apple's Command Line Tools and use Clang to compile and execute C programs.
Running a C program in VS Code becomes straightforward once the compiler and editor are configured correctly. Install MinGW on Windows, update the PATH, add the required extensions and test your setup with a simple Hello, World! program.
Once your first program runs, keep practising with variables, loops, functions and other C concepts. You can also explore PW Skills programming and DSA courses to build on your programming fundamentals and move towards problem-solving with other languages.
