Introduction
C is one of the most powerful and widely used programming languages in the world. Before a C program can run, the source code written by the programmer must be converted into machine-readable instructions through a process called compilation.
In Windows, this is commonly done using the GCC compiler through the Command Prompt (CMD) or Terminal.
This guide explains:
- Installing and verifying GCC
- Creating C source files
- Navigating folders using CMD
- Compiling programs
- Running executables
- Using compiler flags
- Understanding warnings and errors
1. Understanding the Compilation Process
When you write a C program, the code is stored in a .c file. Humans can read this code, but the computer cannot execute it directly.
The compiler converts the code into an executable file (.exe).
Flow of Execution
C Source Code (.c)
β
Compiler (gcc)
β
Executable File (.exe)
β
Program Runs
2. Prerequisites
Before compiling C programs, you must install a C compiler toolchain.
The most common GCC-based toolchains for Windows are:
| Toolchain | Description |
|---|---|
| MinGW | Minimalist GNU for Windows |
| MSYS2 | Modern GCC environment |
| TDM-GCC | Simplified GCC distribution |
3. Installing GCC on Windows
Option 1: Install MinGW
Step 1: Download MinGW
Visit:
Step 2: Install the Toolchain
During installation:
- Select GCC compiler packages
- Install C compiler support
Step 3: Add GCC to PATH
You must add the bin directory to the Windows Environment Variables.
Example:
C:\mingw64\bin
4. Verifying GCC Installation
After installation, confirm GCC is accessible.
Open Command Prompt
Press:
Windows + R
Type:
cmd
Press Enter.
Run Verification Command
gcc --version
Expected Output
If installed correctly:
gcc (MinGW-W64 GCC) 13.2.0
Copyright (C) Free Software Foundation
If You Receive an Error
Example:
'gcc' is not recognized as an internal or external command
This means:
- GCC is not installed
- OR PATH variable is not configured properly
5. Creating Your First C Program
You can use:
- Notepad
- VS Code
- Notepad++
- Sublime Text
- Vim
6. Creating a .c Source File
Step 1: Open Notepad
Press:
Windows + R
Type:
notepad
Step 2: Save the File Properly
Go to:
File β Save As
Important Settings
| Setting | Value |
|---|---|
| File Name | application.c |
| Save as Type | All Files |
| Encoding | UTF-8 |
Why "All Files" Matters
If you leave it as Text Documents:
application.c.txt
This is NOT a valid C source file.
Correct extension:
application.c
7. Writing the C Program
Example program:
#include <stdio.h>
int main() {
printf("hello world");
return 0;
}
8. Understanding the Program Line by Line
Header File
#include <stdio.h>
This imports the Standard Input Output library.
It allows usage of:
printf()scanf()
Main Function
int main()
This is the entry point of every C program.
Execution always begins here.
Printing Output
printf("hello world");
Displays text on the screen.
Return Statement
return 0;
Indicates successful execution.
9. Opening Command Prompt in the Correct Folder
The compiler must access your .c file.
Method 1: Windows 11
Inside the folder:
- Right-click empty space
- Select:
Open in Terminal
Method 2: Older Windows Versions
Inside File Explorer:
- Click the address bar
- Type:
cmd
Press Enter.
CMD opens directly inside that folder.
10. Checking Current Directory
Use:
dir
You should see:
application.c
11. Compiling the Program
Basic Compilation
Command:
gcc application.c
What Happens Internally
GCC:
- Reads the source code
- Checks syntax
- Converts code into machine instructions
- Generates executable file
Output File
Default executable:
a.exe
12. Running the Program
Execute:
a
or
a.exe
Expected Output
hello world
13. Creating Custom Executable Names
Instead of a.exe, you can specify your own filename.
Using the -o Flag
Command:
gcc application.c -o application.exe
Breakdown
| Part | Meaning |
|---|---|
| gcc | Compiler |
| application.c | Source file |
| -o | Output flag |
| application.exe | Output executable |
Running the Custom Executable
application
Output:
hello world
14. Understanding Compiler Flags
Flags modify compiler behavior.
Most Common GCC Flags
| Flag | Purpose |
|---|---|
-o |
Specify output file |
-Wall |
Enable warnings |
-g |
Debug information |
-O2 |
Optimization |
-std=c11 |
Use C11 standard |
15. Using -Wall for Warnings
Command:
gcc -Wall application.c -o app.exe
Why Warnings Matter
Warnings help detect:
- Unused variables
- Missing return values
- Dangerous conversions
- Suspicious code
Example Warning
Code:
#include <stdio.h>
int main() {
int x;
printf("hello");
return 0;
}
Compilation:
gcc -Wall app.c -o app.exe
Warning:
warning: unused variable 'x'
16. Difference Between Warnings and Errors
| Warnings | Errors |
|---|---|
| Program still compiles | Compilation stops |
| Indicates risky code | Indicates invalid code |
| Executable created | No executable created |
17. Example of a Compilation Error
Incorrect code:
#include <stdio.h>
int main() {
printf("hello world")
return 0;
}
Error Explanation
Missing semicolon:
printf("hello world");
Compiler output:
error: expected ';'
No .exe file is generated.
18. Useful CMD Commands for C Development
| Command | Purpose |
|---|---|
dir |
List files |
cd |
Change directory |
cls |
Clear screen |
mkdir |
Create folder |
del |
Delete file |
Example Workflow
Step 1: Navigate
cd Desktop\C
Step 2: Compile
gcc -Wall application.c -o app.exe
Step 3: Run
app
19. Recommended Folder Structure
C-Projects/
β
βββ HelloWorld/
β βββ application.c
β βββ app.exe
β
βββ Calculator/
β βββ calc.c
β βββ calc.exe
20. Best Practices
Always Use Warnings
gcc -Wall
Use Meaningful Filenames
Bad:
a.c
Good:
calculator.c
Keep Source and Executables Organized
Separate:
- Source code
- Build files
- Executables
21. Common Beginner Mistakes
| Mistake | Problem |
|---|---|
Saving as .txt
|
Compiler cannot detect C file |
| Forgetting semicolons | Compilation errors |
| Wrong folder in CMD | File not found |
| GCC not in PATH | Command not recognized |
22. Full Example Session
Source Code
File: hello.c
#include <stdio.h>
int main() {
printf("Hello from C!");
return 0;
}
Compilation
gcc -Wall hello.c -o hello.exe
Execution
hello
Output
Hello from C!
23. Summary
Using GCC in Windows Command Prompt involves four major steps:
- Install GCC
- Write C code
- Compile using
gcc - Run the executable
Core command:
gcc filename.c -o output.exe
Recommended command:
gcc -Wall filename.c -o output.exe
This workflow forms the foundation of C development and helps build a deeper understanding of how programming languages interact with the operating system and hardware.
Short Summary: Compiling and Executing a C Program in Windows
Step 1: Write the C Program
Create a C source file with the .c extension.
Example:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
Save it as:
application.c
Step 2: Open Command Prompt in the File Location
Navigate to the folder containing the .c file.
Example:
cd Desktop\C
Step 3: Compile the Program
Compilation converts human-readable C code into machine-executable code (.exe file).
Case 1: Compile WITHOUT Output Filename
Command:
gcc application.c
What Happens?
- GCC compiles the source code.
- A default executable file is automatically created.
- Default executable name in Windows:
a.exe
Execute the Program
a
or
a.exe
Case 2: Compile WITH Output Filename
Command:
gcc application.c -o application.exe
What Happens?
- GCC compiles the source code.
- The
-oflag tells GCC to create a custom executable name. - Executable created:
application.exe
Execute the Program
application
or
application.exe
Difference Between Both Methods
Without -o
|
With -o
|
|---|---|
| Default executable name | Custom executable name |
Creates a.exe
|
Creates specified filename |
| Less organized | More professional and manageable |
Recommended Method
Use:
gcc application.c -o application.exe
because:
- Easier to identify executables
- Better project organization
- Prevents overwriting
a.exe - Standard industry practice
Top comments (0)