To use a C compiler in VS Code, install a toolchain on your computer, make sure the terminal can find it, and then connect the editor to it. This guide takes you from an empty setup to a compiled C program, a repeatable build task, and working scanf input.
The most useful checkpoint is simple: compile one small source file from the integrated terminal before configuring buttons or a debugger. If that works, you have a working compiler and can investigate editor configuration separately. If it fails, a short terminal command gives you a clearer starting point than several extensions running at once.
1. Understand what you are installing
VS Code edits your source. A compiler such as GCC or Clang translates it, and a linker connects it with library implementations. A debugger lets you pause execution and inspect state. Installing an editor extension does not supply all those tools. Microsoft’s C/C++ extension documentation makes that distinction explicit.
Install VS Code from its official download page. In Extensions, search for C/C++, published by Microsoft; its identifier is ms-vscode.cpptools. Despite the name, it supports C source as well as C++. It provides editor and debugging integration. You do not need a separate Code Runner extension for the workflow below.
| Computer | Toolchain | Check command |
|---|---|---|
| Windows x64 | MSYS2 UCRT64 GCC / MinGW-w64 | gcc --version |
| macOS | Apple Command Line Tools / Clang | clang --version |
| Ubuntu or Debian Linux | GCC from distribution packages | gcc --version |
If your course specifies a toolchain, use that requirement first. This article’s Windows commands are for the x64 UCRT64 environment. Windows ARM64 and other Linux distributions have different installation choices; consult their official instructions rather than mixing package names from unrelated platforms.
2. Windows: install GCC through MSYS2
Install and update MSYS2
Download MSYS2 from the official MSYS2 website and follow the installer. The default folder is C:\msys64. The current installer requires a supported 64-bit Windows version; check the requirements on that page before downloading. The remaining paths assume this default location.
Open MSYS2 UCRT64 from the Start menu. Update packages using the command below. If the updater asks you to close the terminal, finish that step, reopen UCRT64, and run the update again. Follow MSYS2’s update instructions for any required restart.
pacman -SyuIn the MSYS2 terminal, install the toolchain:
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchainAccept the package selection and installation prompts. This toolchain includes GCC and GDB. The command follows Microsoft’s MinGW setup guide; for C files, we will call gcc rather than the g++ command used in its C++ example. Do not enter pacman commands in an ordinary PowerShell terminal.
Add the compiler directory to PATH
In Windows, search for “Edit environment variables for your account.” Edit the user Path variable and add a new entry:
C:\msys64\ucrt64\binKeep the existing entries. Close all VS Code windows, reopen the editor, and choose Terminal → New Terminal. In PowerShell, verify both the version and the executable location:
gcc --version
gdb --version
where.exe gccThe location should include C:\msys64\ucrt64\bin\gcc.exe. If another GCC appears first, you have more than one installation and should select the intended one explicitly. Avoid substituting C:\msys64\usr\bin: that belongs to a different environment. MSYS2’s environment reference explains why UCRT64 and MSYS are distinct.
3. macOS: install the command-line Clang toolchain
First open Terminal and try clang --version. If it reports an Apple Clang version, a compiler is already available. Otherwise request the command-line developer tools:
xcode-select --install
clang --versionComplete the installation dialog before running the version check. You do not need to install the full Xcode application just for the small C programs in this guide. See Apple’s Command Line Tools resources and VS Code’s macOS Clang guide for the official setup path.
Reopen VS Code after installation and check the command in its integrated terminal too. Use clang for the following C builds. Some macOS systems expose a gcc command that is backed by Apple Clang, so inspect the version output rather than assuming the command name identifies the implementation.
4. Linux: install GCC and verify the environment
On Ubuntu or Debian, open a terminal and install the build tools and GDB:
sudo apt-get update
sudo apt-get install build-essential gdb
gcc --version
gdb --versionThese are package-manager commands for those distributions. Fedora, Arch, and other distributions use their own packages and installation commands. Microsoft’s Linux GCC guide documents the Ubuntu setup and compiler checks.
For this local workflow, run the check and the program in the same environment. A compiler installed inside WSL is not automatically a native Windows compiler available to PowerShell. If you choose WSL, work in a VS Code WSL window and use its Linux terminal throughout; use a Windows UCRT64 setup when you want Windows executables.
5. Compile a C program before configuring the editor
Create a folder called c-practice and open it through File → Open Folder. Save a file named hello.c containing the following source. Use the .c extension: naming a file .cpp selects a different language workflow.
#include <stdio.h>
int main() {
printf("Hello, C!\n");
return 0;
}The header declares printf, main contains the executed statements, and return zero reports success. The newline escape finishes the output line. Save the source before compiling; the terminal compiler reads the file on disk, not unsaved editor text.
Windows PowerShell
gcc -std=c17 -Wall -Wextra -g hello.c -o hello.exe
.\hello.exeLinux terminal
gcc -std=c17 -Wall -Wextra -g hello.c -o hello
./hellomacOS terminal
clang -std=c17 -Wall -Wextra -g hello.c -o hello
./helloRun the second line only after the first finishes successfully. In all three cases, the expected program output is Hello, C!. A successful compile commonly produces no terminal message at all. Look for the generated executable, then launch it.
The -std=c17 option chooses the language standard; -Wall -Wextra enables useful warnings; -g adds debugging information; and -o names the output. These commands build and link a single file. Consult GCC’s overall options and warning options for the exact behavior. Warning flags do not establish that a program is correct or memory-safe.
6. Give VS Code a repeatable build task
Once the terminal build works, create .vscode/tasks.json inside your project folder. Use this configuration for the active C file. Change the Windows compiler path if you installed MSYS2 elsewhere.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build active C file",
"type": "process",
"command": "gcc",
"args": [
"-std=c17",
"-Wall",
"-Wextra",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"windows": {
"command": "C:/msys64/ucrt64/bin/gcc.exe",
"args": [
"-std=c17",
"-Wall",
"-Wextra",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
]
},
"osx": {
"command": "clang"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}This task launches the compiler as a process and passes each argument separately. Its Windows override generates an .exe file and uses the known UCRT64 compiler; macOS selects clang; Linux uses gcc from PATH. These platform overrides and the problem matcher are documented in VS Code’s task reference.
Save hello.c, keep it as the active editor, and select Terminal → Run Build Task, or press Ctrl+Shift+B on Windows/Linux and Cmd+Shift+B on macOS. The task compiles the active file. If tasks.json itself is active, you will be passing JSON to the compiler, so select your C tab first.
This task builds an executable; it does not launch it. Run the output from the terminal using the platform command above. Later, a project with several C files needs a build command that lists its sources or a build system. Do not assume that opening several tabs makes this single-file task link them together.
Keep IntelliSense and the build configuration consistent
Open the Command Palette and choose C/C++: Edit Configurations (UI). Set Compiler path to the compiler used by your task and choose C17 as the C standard. This helps the extension understand your environment; it does not rewrite tasks.json. If the terminal build succeeds but the editor marks standard headers as missing, investigate this configuration before changing valid C code.
7. Test scanf in an interactive terminal
Save this as input.c. It prints a prompt, checks one integer conversion, and echoes the value. The flush makes the prompt visible before the read; fprintf sends an invalid-input message to stderr.
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
fflush(stdout);
if (scanf("%d", &number) != 1) {
fprintf(stderr, "That was not an integer.\n");
return 1;
}
printf("You entered %d.\n", number);
return 0;
}Build input.c using the task, then run .\input.exe in PowerShell or ./input on Linux/macOS. Type 42 and press Enter. You should see You entered 42. Run again with a letter to check the rejection path. Use small, representable integer values for this introductory example.
When the program waits, enter data in the terminal that launched it. The Output panel and Debug Console are not substitutes for an stdin-capable terminal. If you use another extension and it runs the program in an output-only panel, change that extension’s execution settings or return to the terminal workflow. Microsoft’s C/C++ FAQ explains the terminal requirement for input during debugging.
To begin debugging, use the C/C++ extension’s build-and-debug action for the active C file and select your installed compiler. Check that its build task uses gcc or clang and that its debugger exists. Keep stdin in an integrated or external terminal. The build task here includes debugging symbols, but it does not by itself create a debugger configuration.
8. Diagnose the setup problem before reinstalling
“gcc is not recognized” or “command not found”
The shell cannot locate the executable. On Windows, try & "C:\msys64\ucrt64\bin\gcc.exe" --version in PowerShell. If that works, check PATH and restart VS Code. If it does not, inspect whether the toolchain package was installed in the expected folder. The ampersand invokes a quoted executable path in PowerShell.
“hello.c: No such file or directory”
Check the terminal’s current folder using pwd, then list its files with dir in PowerShell or ls on Linux/macOS. Save the editor file and move to its directory. Distinguish a missing source path from a broken compiler installation.
The old result runs after an edit
Save and rebuild, then check the compiler’s exit result. If compilation failed, an older executable can still exist on disk. Launching it does not mean the new source compiled. Keep build and run as separate steps until you can recognize that failure.
The terminal works, but the build button fails
Compare the successful command with the task’s command, arguments, working folder, and output path. A stale path in tasks.json is not repaired by changing IntelliSense settings. Ensure hello.c is active and that the output name matches the file you launch.
The program shows a prompt and then waits
A successful read normally waits for input. Click the running terminal and submit a line. If it still waits, inspect the format string and any earlier unread input. For example, trailing whitespace in a scanf format can require another non-whitespace character. See our input guide for a language-level explanation.
A run fails for a reason unrelated to installation
Once Hello World compiles and runs, a missing semicolon, invalid pointer, or undefined function in another exercise needs its own diagnosis. Read the earliest meaningful message and reduce the failing source. Our compiler error guide separates common source mistakes from environment problems.
9. Verify the complete workflow
Your setup is ready for small C exercises when the compiler reports its version in VS Code’s terminal, hello.c builds with no unexplained diagnostics, the new executable prints the greeting, the build task compiles the active file, and input.c accepts a reply in the running terminal. Each checkpoint confirms a different connection; a working extension icon alone confirms none of them.
Next, change the input example into a small calculation or split that calculation into a function. Keep known test inputs and expected results. Add a debugger when you need to inspect variables rather than repeatedly printing them, and move to a project build system when multiple files become necessary.
You can also test a portable exercise in the Circuit online C compiler, then download its .c source for your desktop setup. Circuit uses Clang and WebAssembly rather than a native GCC environment, so check its runtime boundaries before comparing platform-specific code. The browser and desktop compiler comparison explains when each workflow helps.