Why VS Code Code Runner Is Slow for C Programs on Windows (And How to Fix It)
Running a simple "Hello World!" in C shouldn't take five seconds, especially on a capable machine equipped with an Intel Core i7 and 16 GB of RAM. Yet, many Windows developers using Visual Studio Code and the popular Code Runner extension encounter a frustrating delay between hitting "Run Code" and seeing the program output.
If you've verified that GCC compiles your code in a fraction of a second (e.g., around 0.2s via Measure-Command), the issue isn't the compiler or your hardware. The bottleneck almost always comes down to Windows Defender, PowerShell initialization overhead, or Code Runner terminal configurations. Here is how to diagnose and fix it.
1. Switch Code Runner from PowerShell to Command Prompt or Git Bash
By default, VS Code uses PowerShell as its integrated terminal. While powerful, PowerShell has noticeable startup overhead when launching scripts, spawning subshells, and resolving environment paths—often adding 1.5 to 3 seconds of lag.
You can dramatically cut down execution time by setting the default terminal profile to Command Prompt (cmd.exe) or Git Bash:
- Press Ctrl + Shift + P and select Terminal: Select Default Profile.
- Choose Command Prompt or Git Bash instead of PowerShell.
Alternatively, force Code Runner to use a faster shell by updating your settings.json:
{
"terminal.integrated.defaultProfile.windows": "Command Prompt"
}2. Add an Antivirus Exclusion for Your Workspace
Windows Defender (Microsoft Defender Antivirus) uses real-time protection to scan every newly generated .exe file before allowing it to run. When Code Runner runs:
gcc main.c -o main && .\mainWindows Defender immediately intercepts the new main.exe, scans its binary signature, and holds execution until it passes. This check is a prime reason for intermittent multi-second pauses.
How to resolve it:
- Open Windows Security > Virus & threat protection.
- Click Manage settings under Virus & threat protection settings.
- Scroll down to Exclusions and click Add or remove exclusions.
- Add an exclusion for your primary C programming directory (e.g.,
C:\Projects\C) and optionally your MinGWbinfolder.
3. Toggle the "Run in Terminal" Setting
Code Runner has two modes: outputting to the dedicated read-only Output panel or running in an interactive Integrated Terminal.
Spawning a terminal tab incurs process creation overhead. If your program doesn't require keyboard input (like scanf), running directly in the output tab is almost instantaneous:
{
"code-runner.runInTerminal": false,
"code-runner.clearPreviousOutput": true
}Note: If you need user input via scanf() or fgets(), keep "code-runner.runInTerminal": true, but combine it with the Command Prompt profile mentioned above.
4. Optimize the Code Runner Executor Command
By default, Code Runner navigates to the active directory and compiles every single time you trigger it. You can streamline the command in your settings.json under code-runner.executorMap:
{
"code-runner.executorMap": {
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
}
}Ensure your paths do not trigger unnecessary network drives or OneDrive-synced folders, as cloud file syncs (like OneDrive Desktop sync) add file locking delays when generating executables.
5. Better Alternative: Use Native VS Code Tasks
While Code Runner is convenient for quick scripts, it isn't the standard way to build and debug C/C++ in VS Code. The official C/C++ extension by Microsoft (ms-vscode.cpptools) uses native build tasks that persist shell instances and integrate directly with the GDB debugger.
To set it up:
- Open your
.cfile. - Press Ctrl + Shift + B and select C/C++: gcc.exe build active file.
- VS Code will generate a
.vscode/tasks.jsonfile configured directly for GCC without the third-party extension overhead.
Summary Checklist
- Exclude your development folder from Windows Defender's real-time scanner.
- Switch your terminal profile from PowerShell to Command Prompt or Git Bash.
- Disable "Run in Terminal" in Code Runner if interactive input is not required.