Working of C Program
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Working of C Program: The Compilation Process Explained

📑 On this page:
  • What is Compilation?
  • Step 1: Preprocessing
  • Step 2: Compiling
  • Step 3: Assembling
  • Step 4: Linking
  • Compilation Flow Diagram
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is compilation and why it matters
  • What happens during preprocessing
  • How compiling converts code to assembly
  • What assembling does with assembly code
  • How linking creates the final executable
  • The complete compilation flow from .c to .exe

What is Compilation?

C Compiler

A compiler is a program that translates code written in one language (source code) into another language (target code). For C programs, the compiler translates your .c file into an executable .exe file.

Think of it like a translator — you write instructions in English (C code), and the compiler translates them into machine language (0s and 1s) that your computer understands.

💡 Key Point: The compilation process converts hello.c → hello.obj → hello.exe

The Four Steps of Compilation

1.

Preprocessing

.c → .i

2.

Compiling

.i → .s

3.

Assembling

.s → .obj

4.

Linking

.obj → .exe

Step 1: Preprocessing

Preprocessing is the first step. The preprocessor handles all lines starting with # (like #include, #define, #ifdef). It creates an intermediate file with a .i extension.

What happens during preprocessing?

  • Comments Removal: All comments are removed — they're only for humans, not machines.
  • Macros Expansion: Macros like #define PI 3.14 are replaced with their values.
  • File Inclusion: Header files like stdio.h are inserted into the code.
  • Conditional Compilation: Code blocks with #ifdef, #endif are processed.

Example of comments removal:

/* This is a multi-line comment in C */
#include <stdio.h>

int main()
{
    // this is a single-line comment
    printf("Hello World!");
    return 0;
}

Example of Macros Expansion:

/* Defining a value */
#define G 9.8

/* Defining an expression */
#define SUM(a, b) (a + b)

Example of Conditional Compilation:

/* if we uncomment the below line, GRAVITY will be printed */
// #define GRAVITY 18

int main()
{
    /* if GRAVITY is defined then print the value */
    #ifdef GRAVITY
        printf("Gravity is %d", GRAVITY);
    #else
        printf("Not Defined");
    #endif
    return 0;
}
Compilation Process

Step 2: Compiling

The compiling phase takes the preprocessed .i file and converts it to assembly code (a .s file). Assembly code is a low-level, human-readable language that's one step above machine code.

During this phase, the compiler checks for syntax errors. If there are mistakes in your code, the compiler will show error messages in the terminal.

Compiling in C

Step 3: Assembling

Assembling converts the assembly code (.s) into machine code (0s and 1s). The result is an object file with a .obj extension on Windows or .o on UNIX systems.

What is an Assembler? An assembler is a program that translates assembly language into binary code that the computer's processor can understand.

Assembler in C

Step 4: Linking

Linking is the final step. The linker combines your object file with library files (like .lib files) to create the final executable file (.exe on Windows, .out on UNIX).

What is a Linker? A linker is a program that takes object files and library files and combines them into a single executable file.

Linking in C

Compilation Flow Diagram

areaofcircle.c
→
Preprocessing
→
areaofcircle.i
↓
Compiling
→
areaofcircle.s
↓
Assembling
→
areaofcircle.obj
↓
Linking
→
areaofcircle.exe
Complete Compilation Process Flow

🎯 Key Takeaway: The compilation process turns your human-readable C code into a machine-readable executable file. Each step has a specific purpose, and all four are essential.

Frequently Asked Questions About Compilation

1. What is the difference between a compiler and a linker?

A compiler translates source code into machine code. A linker combines multiple object files and libraries into a single executable file.

2. Why do we need preprocessing?

Preprocessing handles directives like #include and #define. It makes code reusable and easier to maintain.

3. What is the difference between .obj and .exe files?

.obj (object file) is the output of assembling — it contains machine code but isn't fully executable. .exe is the final executable file after linking.

4. Can I skip the preprocessing step?

No. Preprocessing is automatic and essential. You can't skip it — the compiler handles it automatically.

💡 Quick tip: Understanding the compilation process helps you debug errors more effectively and appreciate what happens behind the scenes.

📖 Related Tutorials

  • Structure of C Program
  • What is a Program?
  • Data Types in C
  • Operators in C

Previous Topic: -->> Structure of C Program   ||   Next topic: -->> Character Set in C


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.