Introduction

One of the more puzzling aspects of the C and C++ languages for new programmers is the C preprocessor directives, especially in regards to the #include directive. This page is intended to serve as a brief explanation of what the preprocessor is an how it works.

The preprocessor directives, taken as a whole, compose macro language with it's own syntax and semantics; while it is defined as part of the C language standard, it behaves differently from most other C constructs. As the name implies, the preprocessor operates on the source code before the compilation begins. Indeed, many C compiler packages include a separate command-line version of the preprocessor, usually named cpp, which can be applied to source files independently.

The preprocessor macro language is an extremely simple one. In general, it consists of a series of one line directives which either define a set of textual transformations to be made upon the source file, select sections of code to remove from the source stream, or pass an option to the compiler regarding how the code should be compiled. The directives supported by most versions of the preprocessor are:

Source-transformation directives

#include
#define
#undef

Conditional compilation directives

#if
#ifdef
#ifndef
#elif
#else
#endif

Compiler directives:

#error
#line
#pragma

In addition, some compilers implement additional directives, such as #page and #warning, which are not part of the C standard.

The preprocessor performs a pass over the text of the source file before feeding it to the compiler; the source code which the compiler receives has already been altered by the preprocessor, with the effect that the compiler itself is entirely unaware of the transformational preprocessor directives.

In older versions of the preprocessor, the # (hash) mark must be the first non-whitespace character on a line of text containing the directive. Newer compilers do not impose this limitation, but the convention remains widely followed.

Unlike other C expressions, a directive does not need a semi-colon to indicate when it ends; the directive continues until the end of the line of text it begins on, unless a line extension marker is used (see below), in which case it closes with the first line which does not have the marker. Adding a semi-colon to a directive is a common programming mistake, and one which can be difficult to spot.

Most versions of the preprocessor have the added effect of stripping comments out of the code.


Contents Previous Next