Preprocessor

Noun · Development

Definitions

  1. Preprocessor is a program that processes source code before it reaches the compiler, performing text transformations based on special directives. In C and C++, the preprocessor handles directives that begin with #, including #include (inserting the contents of another file), #define (creating macros and constants), #ifdef/#ifndef (conditional compilation), and #pragma (compiler-specific instructions). Preprocessor macros can define constants, create inline function-like expansions, and generate repetitive code patterns. Conditional compilation allows the same source code to be built differently for various platforms, debug versus release modes, or feature flags. While powerful, preprocessor macros operate on raw text without understanding the language syntax, which can lead to subtle bugs from unexpected expansions, especially with complex macro arguments. Modern C++ encourages constexpr, templates, and inline functions as type-safe alternatives to many traditional preprocessor uses.

    In plain English: A program that modifies your source code before the real compiler runs — it handles shortcuts, includes other files, and toggles code on or off based on conditions.

    Example: "The preprocessor expanded that innocent-looking macro into 200 lines of code. Good luck debugging that."

Etymology

1960s
Early macro processors emerged in assembly language programming, allowing symbolic substitution before the assembler ran.
1972
The C programming language formalized the preprocessor as a distinct compilation phase, using directives like #include and #define to transform source code before compilation.
1980s
C preprocessor conventions became widespread across systems programming. Conditional compilation (#ifdef) became essential for cross-platform code.
2000s
CSS preprocessors like Sass (2006) and LESS (2009) borrowed the concept for web development, adding variables and mixins to stylesheets before compilation.

Origin Story

The Tool That Transforms Code Before Compilation

A preprocessor is a program that processes source code before the main compiler runs, performing textual transformations like macro expansion, file inclusion, and conditional compilation. The concept originated with the C programming language at Bell Labs in the early 1970s. The C preprocessor (cpp) was developed by Dennis Ritchie as a separate phase that ran before the actual C compiler. It handled directives beginning with '#': #include to insert file contents, #define to create macros, and #ifdef/#endif for conditional compilation. This design allowed C programs to be portable across different systems by conditionally including platform-specific code. The preprocessor was a pragmatic solution: rather than building these features into the language itself, the text-transformation approach kept the compiler simple. Other languages adopted similar concepts. C++ inherited C's preprocessor wholesale. The m4 macro processor (written by Brian Kernighan and Dennis Ritchie in 1977) was a general-purpose preprocessor used in Unix build systems. CSS preprocessors like Sass (2006) and Less (2009) added variables, nesting, and mixins to CSS long before the language natively supported them. HTML template engines like Jinja2 and Handlebars function as preprocessors for markup. The pattern of 'transform the source before the real tool sees it' remains a powerful technique across the entire software stack.

Context: C programming language, Bell Labs, early 1970s

Fun fact: The C preprocessor is technically a separate program from the C compiler, and it processes plain text with no understanding of C syntax. This means you can (ab)use it to preprocess non-C files, and some projects have used cpp to preprocess Fortran and assembly code.

Related Terms