This guide shows you how to compile and run a C program on Windows 11 using GCC from the Command Prompt. If you have already installed a compiler, you are ready to go. If not, start with our guide on how to install GCC on Windows 11 first — it walks you through MSYS2 and MinGW-w64.
Just need to run one or two quick programs without installing anything? You can compile C in your browser instead — see our guide to online C compilers. No setup required.
The Two-Step Workflow
Running a C program on Windows is always two steps:
- Compile — turn your
.csource file into an.exeexecutable - Run — execute that
.exe
C is a compiled language — you cannot run a .c file directly the way you would a Python script. GCC translates your source code into a Windows executable first.
Step 1 — Write Your C Program
Open Notepad (or any text editor) and type this program:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Save it as hello.c. Important: in Notepad’s Save dialog, set Save as type to All Files — otherwise Notepad saves it as hello.c.txt, which will not compile. Save it somewhere easy to find, such as C:\code\.
Step 2 — Open Command Prompt in the Right Folder
The quickest way: open the folder containing hello.c in File Explorer, click the address bar, type cmd, and press Enter. A Command Prompt opens already pointed at that folder.
Alternatively, open Command Prompt from the Start menu and navigate with cd:
cd C:\code
Confirm your file is there with dir:
dir hello.c
Step 3 — Compile the Program
gcc hello.c -o hello.exe
Breaking this command down:
| Part | Meaning |
|---|---|
gcc |
The MinGW-w64 GCC compiler |
hello.c |
Your source file — the input |
-o hello.exe |
Name the output executable hello.exe |
If your code has no errors, the command finishes silently and creates hello.exe in the same folder. If you omit -o hello.exe, GCC names the output a.exe by default.
Step 4 — Run the Program
hello.exe
Output:
Hello, World!
On Windows you run the program by typing its name. (On macOS and Linux you need a ./ prefix — on Windows you do not, because the current directory is searched automatically.)
The Complete Sequence
cd C:\code
gcc hello.c -o hello.exe
hello.exe
Three commands every time: navigate, compile, run.
Recommended Compiler Flags
For learning C, always compile with warnings enabled — they catch real bugs early:
gcc -Wall -Wextra hello.c -o hello.exe
| Flag | What it does |
|---|---|
-Wall |
Enable all common warnings |
-Wextra |
Enable extra warnings -Wall misses |
-std=c17 |
Use the C17 standard (or -std=c11, -std=c99) |
-g |
Add debug info for use with gdb |
-O2 |
Optimise the compiled program for speed |
A good everyday command for students:
gcc -Wall -Wextra -std=c17 hello.c -o hello.exe
Compiling Multiple Source Files
As programs grow, you split them across files. Compile them together by listing each .c file:
gcc -Wall main.c utils.c math_helpers.c -o myprogram.exe
myprogram.exe
GCC combines all the source files into one executable. Header files (.h) are not listed — they are pulled in automatically by the #include directives in your code.
Reading Input While the Program Runs
If your program uses scanf(), just type the values when it runs:
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("You entered %d\n", n);
return 0;
}
gcc -Wall input.c -o input.exe
input.exe
Enter a number: 42 You entered 42
Common Errors and Fixes
| Error message | Cause and fix |
|---|---|
'gcc' is not recognized as an internal or external command |
GCC not installed or not on PATH — see our install guide |
gcc: error: hello.c: No such file or directory |
Wrong folder — use cd to navigate to the file, check with dir |
File saved as hello.c.txt |
Notepad added .txt — re-save with Save as type → All Files |
undefined reference to 'function' |
You forgot to include a .c file in the compile command — list all of them |
implicit declaration of function 'printf' |
You forgot #include <stdio.h> at the top |
Tired of Typing Commands? Use an IDE
Running three commands for every change gets old fast. VS Code lets you compile and run with a single keypress and adds code completion plus a visual debugger. It is free and works directly with the MSYS2 GCC you installed.
What’s Next
You now know how to compile and run C on Windows 11. Time to practise — browse our full list of C programs with examples, from beginner exercises to sorting algorithms and data structures.
As an Amazon Associate we earn from qualifying purchases.
Recommended Book
Once you are comfortable compiling and running programs, the best book to actually learn the language is The C Programming Language by Brian Kernighan and Dennis Ritchie (K&R). Every example in it compiles and runs with the exact gcc workflow above.