1. Gyanastra
  2. Compiler Design
  3. Phases of a Compiler
  4. Preprocessor: File Inclusion and Macro Expansion
Free lesson, no account needed 5 min read 10 practice questions after it
Asked in past papers5 times
  • GATE CSE1997199619951992
  • UGC NET2010

Compiler Design · Phases of a Compiler

Preprocessor: File Inclusion and Macro Expansion

Overview

The preprocessor is the phase that runs before the compiler proper and performs two main jobs: file inclusion and macro expansion.
It matters because it lets a program reuse entire files and define constants and small function-like replacements once, so changes propagate automatically and function-call overhead is avoided.

Key concepts

  • Preprocessor: the phase that runs before the compiler proper; it performs file inclusion and macro expansion.
  • File inclusion: the preprocessor substitutes the entire contents of the named file in place of the include line in the source program, so the source program literally contains the text of the included file.
  • #define constant: a line of the form #define <name> <value> that gives a name to a constant value, so the value is written once instead of at every place in the program.
  • Macro expansion: the replacement of every use of a #define name by its defined value; changing the value in the #define line is automatically reflected at all places where the name is used.
  • Macros as small functions: macros are small functions for which an actual function call is not wanted, because function calling is overhead while macro calling is simple.

How it works

  1. File inclusion: the preprocessor takes the contents of the named file and substitutes the entire file in place of the include line in the source program.
  2. Define a constant: instead of writing a constant's name/value at every place in the program, write one line #define <name> <value>.
  3. Macro expansion: every use of the name is replaced by the defined value; if the value changes, only the #define line is edited and the change is automatically reflected at all places where the name is used.
  4. Function-like macros: when a small piece of functionality is needed repeatedly, a macro is written instead of a function, since function calling is overhead and macro calling is simple; the preprocessor expands it at each use site.
  5. End of the phase: after the preprocessor phase, the compiler phase begins; the input to the compiler is pure high-level language, i.e., the program no longer contains any hash lines.
Figure. The preprocessor runs before the compiler. File inclusion substitutes an entire file, while macro expansion replaces every use of a define name, so editing one define line updates all uses and macros avoid function call overhead.

Formulas

#define <name> <value>    # defines a constant: <name> is replaced by <value> at every use

Worked examples

Example 1: Constant via #define (interest rate)

  • Suppose the program uses the bank interest value at many places.
  • Write one line: #define INTEREST <value>.
  • Every use of INTEREST in the program is replaced by the defined value during macro expansion.
  • If the interest changes, edit only the #define line; all uses update automatically — no manual editing of every occurrence.

Example 2: Macro instead of a function call

  • A small piece of functionality is needed repeatedly.
  • Option A: write a function and call it — function calling is overhead.
  • Option B: write a macro — macro calling is simple, so the preprocessor expands it at each use site.

Comparison

Feature#define constantMacro (function-like)File inclusion
PurposeGive a name to a constant valueSmall function-like replacementInsert an entire file into the source
Why usedAvoid manually changing the value at all places; change once, reflected everywhereAvoid function-call overhead; macro calling is simpleReuse the whole contents of another file
Done byPreprocessor (macro expansion)Preprocessor (macro expansion)Preprocessor (substitution of the entire file)

Revision summary

  • The preprocessor runs before the compiler phase.
  • The two jobs of the preprocessor are file inclusion and macro expansion.
  • File inclusion substitutes the entire file into the source program.
  • Changing the #define line automatically reflects the change everywhere — this is macro expansion.
  • Macros are used to avoid function-call overhead; macro calling is simple.
  • After preprocessing, the compiler receives pure high-level language with no hash lines.

Previous-year exam insights

  • A macro processor performs macro definition and macro expansion.
    In a two-pass macro processor, pass 1 reads the macro definitions into the macro-definition table (MDT/MNT) and pass 2 expands the macro calls; if the macro preprocessor runs before the assembler instead, all expansion is finished before the assembler's own pass 1.
    In both designs, definitions must be seen before the calls that use them are expanded, and expansion is textual substitution independent of symbol resolution.
    Asked inGATE 1992
  • In a macro definition, the names listed after the macro name (e.g., the parameters in 'macro Add x, y') are formal parameters; the values supplied at the macro call site are actual parameters (arguments).
    Asked inGATE 1995
  • A macro processor is a software program (system software), not hardware, a compiler, or a register.
    Asked inUGC NET 2010
  • The conditional expansion facility of a macro processor lets it expand certain model statements depending on the value of a condition at macro-expansion (assembly) time, not at run time of the expanded program.
    Asked inGATE 1997
  • A recursive macro whose expansion re-invokes itself with an argument that keeps satisfying the expansion condition (e.g., incrementing/decrementing without a terminating base case) causes infinite macro expansion; a macro that re-invokes itself with the same argument under an unsatisfiable guard does not loop.
    Asked inGATE 1996
  • Macro expansion is textual substitution performed before compilation/assembly; unlike a function call it involves no call/return overhead, but it can cause code bloat and does not type-check arguments.
Pages 1–2 of 2