|
|
coherent
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:
#iiff Include code if a condition is true
#eelliiff Include code if directive is true
#eellssee Include code if preceding directives fail
#eennddiiff End of code to be included conditionally
#iiffddeeff Include code if a given macro is defined
#iiffnnddeeff Include code if a given macro is not defined
#ddeeffiinnee Define a macro
#uunnddeeff Undefine a macro
#iinncclluuddee Read another file and include it
#lliinnee 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:
_ _DDAATTEE_ _Date of translation
_ _FFIILLEE_ _Source-file name
_ _LLIINNEE_ _Current line within source file
_ _SSTTDDCC_ _Conforming translator and level
_ _TTIIMMEE_ _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 #ddeeffiinnee _i_d_e_n_t_i_f_i_e_r
_v_a_l_u_e. 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 ssttddoouutt.
If the -E option is not used, cpp also writes into its output
statements of the form #lliinnee _n _f_i_l_e_n_a_m_e, 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
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.