C preprocessor Overview C preprocessor
Preprocessing encompasses all tasks that logically precede the
translation of a program. The preprocessor processes headers,
expands macros, and conditionally includes or excludes source
code.
***** Directives *****
The C preprocessor recognizes the following directives:
#if Include code if a condition is true
#elif Include code if directive is true
#else Include code if preceding directives fail
#endif End of code to be included conditionally
#ifdef Include code if a given macro is defined
#ifndef Include code if a given macro is not defined
#define Define a macro
#undef Undefine a macro
#include Read another file and include it
#line Reset current line number
A preprocessing directive is always introduced by the `#' charac-
ter. The `#' must be the first non-white space character on a
line, but it may be preceded by white space and it may be
separated from the directive name that follows it by one or more
white space characters.
***** Preprocessing Operators *****
The Standard defines two operators that are recognized by the
preprocessor: the ``stringize'' operator #, and the ``token-
paste'' operator ##.
The operator # indicates that the following argument is to be
replaced by a string literal; this literal names the preproces-
sing token that replaces the argument. For example, consider the
macro:
#define display(x) show((long)(x), #x)
When the preprocessor reads the line
COHERENT Lexicon Page 1
C preprocessor Overview C preprocessor
display(abs(-5));
it replaces it with the following:
show((long)(abs(-5)), "abs(-5)");
The ## operator performs ``token pasting'' -- that is, it joins
two tokens together, to create a single token. For example, con-
sider the macro:
#define printvar(x) printf("%d\n", variable ## x)
When the preprocessor reads the line
printvar(3);
it translates it into:
printf("%d\n", variable3);
In the past, token pasting had been performed by inserting a
comment between the tokens to be pasted. This no longer works.
***** Predefined Macros *****
The ANSI Standard describes the following macros that must be
recognized by the preprocessor:
_ _DATE_ _Date of translation
_ _FILE_ _Source-file name
_ _LINE_ _Current line within source file
_ _STDC_ _Conforming translator and level
_ _TIME_ _Time of translation
For more information on any one of these macros, see its entry.
***** Conditional Inclusion *****
The preprocessor will conditionally include lines of code within
a program. The directives that include code conditionally are
defined in such a way that you can construct a chain of inclusion
directives to include exactly the material you want.
COHERENT Lexicon Page 2
C preprocessor Overview C preprocessor
***** Macro Definition and Replacement *****
The preprocessor performs simple types of macro replacement. To
define a macro, use the preprocessor directive #define identifier
value. The preprocessor scans the translation unit for
preprocessor tokens that match identifier; when one is found, the
preprocessor substitutes value for it.
***** cpp *****
Under COHERENT, C preprocessing is done by the program cpp. The
cc command runs cpp as the first step in compiling a C program.
cpp can also be run by itself.
cpp reads each input file; it processes directives, and writes
its product on stdout.
If the -E option is not used, cpp also writes into its output
statements of the form #line n filename, so that the parser cc0
can connect its error messages and debugger output with the
original line numbers in your source files.
See the Lexicon entry on cpp for more information.
***** See Also *****
C language, cc, cpp
COHERENT Lexicon Page 3