

Visual Studio Code, usually called VS Code, is one of the most popular code editors among students and developers. It is free, lightweight and works on Windows, macOS and Linux.
If you are learning C programming, VS Code can be a useful place to write, run and debug your programs. However, unlike some IDEs, VS Code does not include a C compiler by default. You need to set up a few tools before you can run your first program.
In this article, you will learn how to set up C programming in VS Code, run your first program, debug your code and fix common errors.
Before you begin, make sure you have the following:
| Requirement | Purpose |
| VS Code | Code editor where you write your programs |
| C compiler (GCC or Clang) | Converts your C code into a program your computer can run |
| C/C++ extension by Microsoft | Adds code suggestions, error highlighting and debugging support |
| Code Runner extension (optional) | Lets you run a program with one click |
VS Code helps you write and manage code, while the compiler turns that code into an executable program. Both are required.
Windows does not include a C compiler, so you need to install one first. A common option is GCC through MSYS2.
Follow these steps:
1. Download VS Code from the official Visual Studio Code website.
2. Run the installer and keep the default settings.
3. Select the option to add VS Code to PATH, if shown.
4. Open VS Code after the installation is complete.
Follow these steps:
1. Download and install MSYS2 from its official website.
2. Open the MSYS2 UCRT64 terminal.
3. Run the command: pacman -S mingw-w64-ucrt-x86_64-gcc
4. Press Enter to confirm the installation.
Windows needs to know where the compiler is installed.
1. Search for Environment Variables in the Start menu.
2. Open Edit the system environment variables.
3. Select Environment Variables, then select Path under User variables.
4. Add the folder where gcc.exe is installed, usually C:\msys64\ucrt64\bin.
5. Save the changes and restart VS Code.
Open Command Prompt and run: gcc --version
If the installation is successful, you will see the GCC version number. If you see an error, check the PATH setting again.
The setup is usually simpler on macOS and Linux.
1. Open Terminal.
2. Run: xcode-select --install
3. Follow the prompts to install Xcode Command Line Tools.
4. Check the installation with: clang --version
1. Open Terminal.
2. Run: sudo apt update
3. Run: sudo apt install build-essential gdb
4. Check the installation with: gcc --version
The C/C++ extension by Microsoft adds features such as IntelliSense, code navigation and debugging.
1. Open VS Code.
2. Select the Extensions icon in the left sidebar, or press Ctrl+Shift+X.
3. Search for C/C++.
4. Select the extension published by Microsoft.
5. Select Install.
You may also install the Code Runner extension if you want to run programs with a single click. However, Code Runner shows output in the Output panel by default, which does not accept keyboard input. If your program uses scanf, enable the Run In Terminal setting in Code Runner.
Once the setup is complete, you can create and run your first program.
Create a new folder, for example C-Programs, then open it in VS Code using File and Open Folder.
Create a new file and name it hello.c. Make sure the file ends with .c.
Type the following code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Open the terminal in VS Code by selecting Terminal and New Terminal. Then run:
1. Compile: gcc hello.c -o hello
2. Run on Windows: .\hello.exe
3. Run on macOS or Linux: ./hello
If everything is set up correctly, the terminal will display Hello, World!
Writing your first program is only the beginning. Practise with more examples in our C programming examples guide.
Debugging helps you find errors by running your program line by line.
1. Open your .c file.
2. Click to the left of a line number to add a breakpoint.
3. Select Run and Debug from the left sidebar, or press F5.
4. Choose the C/C++: gcc build and debug active file option when prompted.
5. Use the controls to step through the code and check variable values.
VS Code creates a tasks.json and launch.json file in a .vscode folder to save these settings for your project.
| Error | Likely cause | How to fix it |
| gcc is not recognised as an internal or external command | Compiler not installed or not added to PATH | Install GCC and add its bin folder to PATH, then restart VS Code |
| #include errors detected | VS Code cannot find the compiler or header files | Select the correct compiler path in C/C++ settings |
| Program does not accept input | Running in the Output panel | Run the program in the terminal or enable Run In Terminal in Code Runner |
| Permission denied | Running the program without execute permission on macOS or Linux | Compile again or run chmod +x on the file |
| Old output appears | Program not recompiled after changes | Save the file and compile again before running |
| Action | Windows / Linux | macOS |
| Open terminal | Ctrl + ` | Cmd + ` |
| Save file | Ctrl + S | Cmd + S |
| Start debugging | F5 | F5 |
| Format code | Shift + Alt + F | Shift + Option + F |
| Command Palette | Ctrl + Shift + P | Cmd + Shift + P |
| Comment a line | Ctrl + / | Cmd + / |
VS Code can be a good choice for learning C because it is free, lightweight and widely used in the industry. Learning to compile programs from the terminal can also help you understand how C code becomes an executable program.
However, the initial setup can take some time for beginners. If you only want to test small programs quickly, an online C compiler may be easier at first. Once you are comfortable, VS Code can help you work on larger projects.
Once you can run programs comfortably, build your skills with our C program for pattern examples and the top C project ideas for beginners.
Setting up C programming in VS Code requires a compiler, the C/C++ extension and a few configuration steps. Once the setup is complete, you can write, compile, run and debug C programs from one place.
Start with simple programs, practise regularly and learn to read compiler errors. These habits can help you build a strong foundation in C and prepare for more advanced programming. Strengthen your basics further with our C programming guide.
