- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Compilation Process
![]() Share with a Friend |
C Programming - C Compilation Process
The compilation process in C involves transforming the high-level source code written by the programmer into machine code that can be executed by a computer. This process is performed in several stages by the compiler. Below is an explanation of the stages:
- Preprocessing
- Purpose: The preprocessor handles directives in the source code, such as #include and #define.
- Tasks Performed:
- Includes header files into the source code.
- Replaces macros with their values.
- Handles conditional compilation directives (#ifdef, #endif).
- Output: A preprocessed file with the .i extension.
- Tool: Preprocessor (e.g., gcc -E).
Example:
#include <stdio.h> // Preprocessor directive #define PI 3.14 // Defines a constant for Pi int main() { printf("Value of PI: %f\n", PI); // Print statement return 0; // Exit the program }
After preprocessing, PI is replaced with 3.14, and the <stdio.h> content is included.
- Compilation
- Purpose: Converts the preprocessed code into assembly language code.
- Tasks Performed:
- Translates C code into an intermediate assembly language representation.
- Checks for syntax and semantic errors.
- Output: An assembly file with the .s extension.
- Tool: Compiler proper (e.g., gcc -S).
- Assembly
- Purpose: Converts assembly code into machine code (binary format).
- Tasks Performed:
- Assembler translates assembly language instructions into opcodes.
- Produces object code specific to the machine's architecture.
- Output: An object file with the .o (or .obj) extension.
- Tool: Assembler (e.g., as).
- Linking
- Purpose: Combines object files and libraries into a single executable file.
- Tasks Performed:
- Resolves external symbols (e.g., functions declared in libraries).
- Links all dependencies (e.g., standard library functions like printf).
- Generates the final executable.
- Output: An executable file, often without an extension (e.g., a.out) or with .exe on Windows.
- Tool: Linker (e.g., ld).
Detailed Flow Diagram
The process can be visualized as:
- Source Code (.c)
↓ Preprocessor - Preprocessed Code (.i)
↓ Compiler - Assembly Code (.s)
↓ Assembler - Object Code (.o)
↓ Linker - Executable File (a.out or .exe)
Commands to Demonstrate the Process
- Preprocessing:
gcc -E program.c -o program.i
Output: Preprocessed file program.i.
- Compilation:
gcc -S program.i -o program.s
Output: Assembly code program.s.
- Assembly:
gcc -c program.s -o program.o
Output: Object file program.o.
- Linking:
gcc program.o -o program
Output: Executable file program.
Errors Encountered During Compilation
- Preprocessing Errors:
- Missing or incorrect header files.
- Undefined macros.
- Compilation Errors:
- Syntax errors.
- Type mismatch.
- Linking Errors:
- Undefined references (e.g., missing function definitions).
- Library not found.
- Runtime Errors:
- Occur after successful compilation and linking (e.g., segmentation faults).
Key Points
- Multi-Step Process: Compilation involves four key steps: preprocessing, compiling, assembling, and linking.
- Tools: Each step uses specialized tools (preprocessor, compiler, assembler, linker).
- Output: The final output is an executable file that can run on the target machine.
This compilation process is standard for many programming languages and is crucial for transforming human-readable code into a format that the computer can execute.
