C preprocessor

From ArticleWorld


The C preprocessor (usually abbreviated as cpp) is the preprocessor of the C language, a program invoked to handle special directives like #include or #define. Although it is mostly used by C/C++ compilers, the C preprocessor is not necessarily tied to it, and, as its syntax is not specific to the C programming language itself, it can be used by other programs as well.

The traditional method used by its implementations is to use a four-phase transformation, with phases occurring consecutively, one at a time; more recent implementation blend these phases, but they must appear as consecutive and one at a time to the compiler.

The four phases

  1. The replacement of trigraphs. The preprocessor finds all trigraph sequences and replaces them with the relevant characters, whenever trigraphs are actually used.
  2. The line splicing. The preprocessor looks for all the lines continued with an escaped newline, splicing them so that they make up logical lines
  3. The tokenizing. The result of the two first phases is tokenized and converted to a series of preprocessing tokens; the comments are replaced by newlines
  4. The Macro Expansion and the Directive Handling. In this phase, the macros are expanded completely, along with the _Pragma operators.

Usage

The C preprocessor is a fundamental tool in C programming. In fact, the first C statement a programmer learns is actually a preprocessor statement: the #include statement. The statement:

#include <unistd.h> 

will be translated by the C preprocessor by actually copying the contents of unistd.h to the file, so that any macro or function defined in the unistd.h file is available to the file that includes it.

Another important use of the C preprocessor is to decide what headers and functions will be used at compile time. For example:

#define __SDL__
#ifdef __SDL__
#include <sdl.h>
#else
#include <opengl.h>
#endif

Macros are often used in C programming. The C preprocessor will expand the encountered macros into their defined form. For example:

#define AGE 27

defines a simple, object-like macro denoted by "AGE". When used in a program, AGE will be replaced by 27, so:

cout << AGE

is equivalent to:

cout << 27

More complex macros can be defined as well, including macros expandable to functions.