DEV Community

Cover image for Compile process on C in Windows
Shiva Charan
Shiva Charan

Posted on

Compile process on C in Windows

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
Enter fullscreen mode Exit fullscreen mode

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:

MinGW Official Website

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
Enter fullscreen mode Exit fullscreen mode

4. Verifying GCC Installation

After installation, confirm GCC is accessible.

Open Command Prompt

Press:

Windows + R
Enter fullscreen mode Exit fullscreen mode

Type:

cmd
Enter fullscreen mode Exit fullscreen mode

Press Enter.


Run Verification Command

gcc --version
Enter fullscreen mode Exit fullscreen mode

Expected Output

If installed correctly:

gcc (MinGW-W64 GCC) 13.2.0
Copyright (C) Free Software Foundation
Enter fullscreen mode Exit fullscreen mode

If You Receive an Error

Example:

'gcc' is not recognized as an internal or external command
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Type:

notepad
Enter fullscreen mode Exit fullscreen mode

Step 2: Save the File Properly

Go to:

File β†’ Save As
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This is NOT a valid C source file.

Correct extension:

application.c
Enter fullscreen mode Exit fullscreen mode

7. Writing the C Program

Example program:

#include <stdio.h>

int main() {
    printf("hello world");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

8. Understanding the Program Line by Line

Header File

#include <stdio.h>
Enter fullscreen mode Exit fullscreen mode

This imports the Standard Input Output library.

It allows usage of:

  • printf()
  • scanf()

Main Function

int main()
Enter fullscreen mode Exit fullscreen mode

This is the entry point of every C program.

Execution always begins here.


Printing Output

printf("hello world");
Enter fullscreen mode Exit fullscreen mode

Displays text on the screen.


Return Statement

return 0;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Method 2: Older Windows Versions

Inside File Explorer:

  • Click the address bar
  • Type:
cmd
Enter fullscreen mode Exit fullscreen mode

Press Enter.

CMD opens directly inside that folder.


10. Checking Current Directory

Use:

dir
Enter fullscreen mode Exit fullscreen mode

You should see:

application.c
Enter fullscreen mode Exit fullscreen mode

11. Compiling the Program

Basic Compilation

Command:

gcc application.c
Enter fullscreen mode Exit fullscreen mode

What Happens Internally

GCC:

  1. Reads the source code
  2. Checks syntax
  3. Converts code into machine instructions
  4. Generates executable file

Output File

Default executable:

a.exe
Enter fullscreen mode Exit fullscreen mode

12. Running the Program

Execute:

a
Enter fullscreen mode Exit fullscreen mode

or

a.exe
Enter fullscreen mode Exit fullscreen mode

Expected Output

hello world
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Breakdown

Part Meaning
gcc Compiler
application.c Source file
-o Output flag
application.exe Output executable

Running the Custom Executable

application
Enter fullscreen mode Exit fullscreen mode

Output:

hello world
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Compilation:

gcc -Wall app.c -o app.exe
Enter fullscreen mode Exit fullscreen mode

Warning:

warning: unused variable 'x'
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Error Explanation

Missing semicolon:

printf("hello world");
Enter fullscreen mode Exit fullscreen mode

Compiler output:

error: expected ';'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 2: Compile

gcc -Wall application.c -o app.exe
Enter fullscreen mode Exit fullscreen mode

Step 3: Run

app
Enter fullscreen mode Exit fullscreen mode

19. Recommended Folder Structure

C-Projects/
β”‚
β”œβ”€β”€ HelloWorld/
β”‚   β”œβ”€β”€ application.c
β”‚   └── app.exe
β”‚
β”œβ”€β”€ Calculator/
β”‚   β”œβ”€β”€ calc.c
β”‚   └── calc.exe
Enter fullscreen mode Exit fullscreen mode

20. Best Practices

Always Use Warnings

gcc -Wall
Enter fullscreen mode Exit fullscreen mode

Use Meaningful Filenames

Bad:

a.c
Enter fullscreen mode Exit fullscreen mode

Good:

calculator.c
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Compilation

gcc -Wall hello.c -o hello.exe
Enter fullscreen mode Exit fullscreen mode

Execution

hello
Enter fullscreen mode Exit fullscreen mode

Output

Hello from C!
Enter fullscreen mode Exit fullscreen mode

23. Summary

Using GCC in Windows Command Prompt involves four major steps:

  1. Install GCC
  2. Write C code
  3. Compile using gcc
  4. Run the executable

Core command:

gcc filename.c -o output.exe
Enter fullscreen mode Exit fullscreen mode

Recommended command:

gcc -Wall filename.c -o output.exe
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

Save it as:

application.c
Enter fullscreen mode Exit fullscreen mode

Step 2: Open Command Prompt in the File Location

Navigate to the folder containing the .c file.

Example:

cd Desktop\C
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

What Happens?

  • GCC compiles the source code.
  • A default executable file is automatically created.
  • Default executable name in Windows:
a.exe
Enter fullscreen mode Exit fullscreen mode

Execute the Program

a
Enter fullscreen mode Exit fullscreen mode

or

a.exe
Enter fullscreen mode Exit fullscreen mode

Case 2: Compile WITH Output Filename

Command:

gcc application.c -o application.exe
Enter fullscreen mode Exit fullscreen mode

What Happens?

  • GCC compiles the source code.
  • The -o flag tells GCC to create a custom executable name.
  • Executable created:
application.exe
Enter fullscreen mode Exit fullscreen mode

Execute the Program

application
Enter fullscreen mode Exit fullscreen mode

or

application.exe
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

because:

  • Easier to identify executables
  • Better project organization
  • Prevents overwriting a.exe
  • Standard industry practice

Top comments (0)