How to Compile and Run C Programs in VS Code

๐Ÿš€ How to Compile and Run C Programs in VS Code (with Code Runner)

Want to write and run C code easily using Visual Studio Code? In this blog, I'll walk you through the full process step-by-step — from setting up your compiler to configuring VS Code for smooth execution.

๐Ÿ“น Watch the Full Tutorial on YouTube:

๐Ÿ›  Step-by-Step Setup Process:

  1. Download and install MinGW from the official SourceForge page:
    ๐Ÿ‘‰ https://sourceforge.net/projects/mingw/
  2. Add bin folder (usually C:\MinGW\bin) to your system's PATH environment variable.
  3. Install the Code Runner extension by Jun Han in VS Code.
  4. Open your VS Code settings.json and add the following code:
"code-runner.executorMap": {
    "c": "cd $dir; gcc $fileName -o $fileNameWithoutExt; if ($?) { .\\$fileNameWithoutExt.exe }"
}

This configuration tells Code Runner to compile your C file and execute it automatically using GCC.

๐Ÿงช For Manual Compilation (Optional):

If you prefer compiling manually via terminal, use the following commands:

gcc ".\01_first.c" -o main
.\main.exe

✅ You're all set!

Now you can create any .c file in VS Code, and run it using either the Code Runner "Run" button or your terminal. Happy coding! ๐ŸŽ‰


๐Ÿ“ฑ Stay Connected:

Comments