Main Page | See live article | Alphabetical index

Preprocessor

A preprocessor is a program that takes text and performs lexical conversions on it. The conversions may include macro substitution, conditional inclusion, and inclusion of other files.

The C programming language has a preprocessor that performs the following transformations:

  1. Replaces trigraphss with equivalents.
  2. Concatenates source lines.
  3. Replaces comments with whitespace.
  4. Reacts to lines starting with an octothorp (#), performing macro substitution, file inclusion, conditional inclusion, and other transformations.

Overuse of the C preprocessor is considered bad style, especially in C++. Stroustrup introduced features such as templates into C++ in an attempt to make the C preprocessor irrelevant. However, his file inclusion alternative was never seriously considered as it was a poor imitation of the C preprocessor's file inclusion mechanism.

Other preprocessors include m4 and Oracle Pro*C. The m4 preprocessor is general-purpose; Oracle Pro*C converts embedded PL/SQL into C.

Preprocessing can be quite cumbersome in incremental parsing or incremental lexical analysis because changes to preprocessing rules can affect the entire text to be preprocessed.

C Example

A typical example in C is:

\r\n#include \r\n\r\nint main (void)\r\n{\r\n    printf("Hello, world!\\n");\r\n    return 0;\r\n}\r\n
The preprocessor replaces the line #include <stdio.h> with the system header file of that name, which facilitates use of the printf() function.