What is a Preprocessor in C?
The preprocessor is a tool that runs before the actual compilation begins. It handles all the preprocessing directives, which are lines in your code that start with a # symbol. These directives are instructions to the compiler to perform specific tasks before compiling the code.
Common Preprocessor Directives
- #include (file inclusion): Adds contents from another file.
- #define: Defines constants or macros.
- #ifdef, #ifndef, #endif: Conditional compilation.
Example:
#include <stdio.h> #define QUIZ_SCORE 100 int main() { printf("Score: %d\n", QUIZ_SCORE); return 0; }
The preprocessor will replace QUIZ_SCORE with 100 before the compiler sees the code.
What is a Compiler in C?
The compiler is a program that converts your human-readable C code into machine code (binary code) that the computer can understand and execute.
It starts working after the preprocessing step is complete.
Compilation Stages:
- Lexical Analysis – Breaks code into tokens.
- Syntax Analysis – Checks for grammar and structure.
- Semantic Analysis – Ensures the code makes logical sense.
- Code Optimization – Improves performance.
- Code Generation – Converts to machine code.
Step-by-Step Compilation Flow
Here’s a simplified version of the C compilation process:
Source Code (.c file) ↓ Preprocessor ↓ Modified Code (no # directives) ↓ Compiler ↓ Assembly Code ↓ Assembler ↓ Object Code (.o file) ↓ Linker ↓ Executable File (.exe or a.out)
Difference between Preprocessor and Compiler in C
Feature | Preprocessor | Compiler |
---|---|---|
Purpose | Prepares code for compilation | Translates code into machine language |
Processes | Handles directives like #define, #include | Performs syntax and semantic analysis |
Execution Time | Before compilation | During compilation |
Error Detection | Limited (e.g., missing files) | Extensive (syntax, semantics, etc.) |
Output | Preprocessed code | Executable machine code |
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Apply for C Internship
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs