Monday, January 26, 2026
HomeTech PostHow to run first C Program using Visual Studio Code and MinGW

How to run first C Program using Visual Studio Code and MinGW

Here’s a step-by-step guide to run your first C program in Visual Studio Code (VS Code) using MinGW (Minimalist GNU for Windows).


🧰 Step 1: Install the Required Tools

1. Install MinGW

  1. Go to the official MinGW website:
    👉 https://www.mingw-w64.org/
  2. Download the mingw-w64 installer (for your Windows version).
  3. Run the installer:
    • Architecture: x86_64 (for 64-bit systems)
    • Threads: posix
    • Exception: seh
    • Installation directory: C:\mingw-w64
  4. Wait for installation to complete.

2. Add MinGW to System PATH

You need to make GCC (the C compiler) available from any terminal.

  1. Open Start Menu → Edit the system environment variables.
  2. Click Environment Variables.
  3. Under System variables, find PathEditNew.
  4. Add: C:\mingw-w64\bin
  5. Click OK on all dialogs.

Verify the installation:
Open Command Prompt and type:

gcc --version

You should see GCC version info (e.g., gcc (x86_64-posix-seh, Built by MinGW-W64 project)).


3. Install Visual Studio Code

If not installed yet, download and install:
👉 https://code.visualstudio.com/


4. Install C/C++ Extensions in VS Code

Open VS Code → Go to Extensions (Ctrl+Shift+X) → Search and install:

  • C/C++ (by Microsoft)
  • (Optional) Code Runner (to run C programs easily)

🧑‍💻 Step 2: Create and Run Your First C Program

1. Create a new folder

Example: C:\C_Programs\HelloWorld

2. Open it in VS Code

  • In VS Code → File → Open Folder → select your folder.

3. Create a new file named:

hello.c

4. Write your first C program:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

⚙️ Step 3: Compile and Run the Program

Option 1: Use VS Code Terminal

  1. Open the terminal in VS Code: Ctrl + `
  2. Compile the code: gcc hello.c -o hello (This creates an executable file named hello.exe.)
  3. Run the executable: ./hello ✅ Output: Hello, World!

Option 2: (Optional) Use Code Runner Extension

If you installed Code Runner, you can simply:

  • Right-click inside your code and choose Run Code.
  • Or press Ctrl + Alt + N.

Output appears in the OUTPUT tab.


🧩 Step 4: (Optional) Setup a Build Task for One-Key Build & Run

If you want to press one shortcut to build and run:

  1. Press Ctrl + Shift + B → Choose C/C++: gcc build active file.
  2. It creates a .vscode/tasks.json file.
  3. Modify it to automatically run your program after build if you want.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments