CD Notes
Learn about preprocessors, macro expansion, file inclusion, and conditional compilation in detail
The preprocessor is the first component in the language processing system. It performs preliminary transformations on the source code before it reaches the actual compiler. Think of it as a text processing stage that handles directives, macro expansions, and file inclusions.
What is a Preprocessor?
A preprocessor is a program that processes source code before compilation. It reads directives (commands starting with special symbols like # in C/C++) and performs textual manipulations on the source file. The output is a modified source file that the compiler can understand.
Common Preprocessor Directives
File Inclusion
The #include directive includes the contents of another file:
#include <stdio.h> // System header files
#include "myheader.h" // User-defined header filesBefore Preprocessing:
#include <stdio.h>
int main() {
printf("Hello\n");
return 0;
}After Preprocessing (conceptually):
// All of stdio.h contents inserted here...
int main() {
printf("Hello\n");
return 0;
}Macro Definition and Expansion
Macros are templates that get replaced with their definitions:
#define MAX 100
#define ADD(a, b) ((a) + (b))
#define SQUARE(x) ((x) * (x))Example:
#define PI 3.14159
#define AREA(r) (PI * (r) * (r))
int main() {
double radius = 5;
double area = AREA(radius); // Expands to: PI * radius * radius
return 0;
}Expansion Process:
Conditional Compilation
These directives allow including or excluding code based on conditions:
#ifdef DEBUG
#define LOG(x) printf("%s\n", x)
#else
#define LOG(x) // Empty macro
#endif
#if defined(WINDOWS)
// Windows-specific code
#elif defined(LINUX)
// Linux-specific code
#endifReal Example:
Output (DEBUG=1):
Output (DEBUG=0):
Preprocessor Workflow
Important Preprocessor Directives Reference
| Directive | Purpose | Example |
|---|---|---|
#include <file> | Include system header | #include <stdio.h> |
#include "file" | Include user header | #include "mylib.h" |
#define NAME VALUE | Define macro | #define SIZE 10 |
#define FUNC(x) expr | Define function-like macro | #define SQ(x) ((x)*(x)) |
#undef NAME | Undefine macro | #undef SIZE |
#ifdef NAME | If defined | #ifdef DEBUG |
#ifndef NAME | If not defined | #ifndef SAFETY_H |
#if condition | If condition true | #if VERSION > 2 |
#else | Else clause | Used with #ifdef |
#elif | Else if | #elif OS == LINUX |
#endif | End conditional | Ends #if block |
#error msg | Compiler error | #error "Old version" |
#pragma | Compiler-specific | #pragma once |
Dangers of Macros
Macros can lead to unexpected behavior if not carefully written:
// BAD: Without parentheses
#define BAD_SQUARE(x) x * x
int result = BAD_SQUARE(3 + 2); // Expands to: 3 + 2 * 3 + 2 = 11 (WRONG!)
// GOOD: With proper parentheses
#define GOOD_SQUARE(x) ((x) * (x))
int result = GOOD_SQUARE(3 + 2); // Expands to: ((3 + 2) * (3 + 2)) = 25 (CORRECT!)Modern Alternatives
Modern languages and compilers prefer compile-time constants and templates over macros:
Interview Q&A
Q: What is the primary role of a preprocessor? A: The preprocessor performs textual transformations on source code before compilation. It handles file inclusions, macro expansions, and conditional compilation directives.
Q: What's the difference between #include <file> and #include "file"? A: <file> searches for system/standard header files in system directories, while "file" searches in the current directory first, then in system directories.
Q: Why are macros dangerous? A: Macros perform simple textual substitution without type checking. They can cause unexpected behavior due to operator precedence issues and lack of scoping.
Q: What does #pragma once do? A: It prevents a header file from being included multiple times in the same compilation unit, effectively preventing duplicate definitions.
Q: How does macro expansion happen? A: The preprocessor performs simple token replacement. Every occurrence of the macro name is replaced with its definition, respecting token boundaries.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for The Preprocessor.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, language, processing, system, preprocessor
Related Compiler Design Topics