16
The most common visible consequence of this is surprising line numbers in error messages.
There is no restriction on what can go in a macro body provided it decomposes into valid preprocessing
tokens. Parentheses need not balance, and the body need not resemble valid C code. (If it does not,
you may get error messages from the C compiler when you use the macro.)
The C preprocessor scans your program sequentially. Macro definitions take effect at the place you
write them. Therefore, the following input to the C preprocessor
foo = X;
#define X 4
bar = X;
produces
foo = X;
bar = 4;
When the preprocessor expands a macro name, the macro's expansion replaces the macro invocation,
then the expansion is examined for more macros to expand. For example,
#define TABLESIZE BUFSIZE
#define BUFSIZE 1024
TABLESIZE
==> BUFSIZE
==> 1024
is expanded first to produce
TABLESIZE
result,
.
1024
Notice that
BUFSIZE
uses exactly the expansion you specify--in this case,
too contains macro names. Only when you use
more macro names.
This makes a difference if you change the definition of
, defined as shown, will always expand using the definition of
TABLESIZE
in effect:
#define BUFSIZE 1020
#define TABLESIZE BUFSIZE
#undef BUFSIZE
#define BUFSIZE 37
Now
expands (in two stages) to
TABLESIZE
If the expansion of a macro contains its own name, either directly or via intermediate macros, it is not
expanded again when the expansion is examined for more macros. This prevents infinite recursion.
Section 3.10.5 Self-Referential Macros, for the precise details.
BUFSIZE
was not defined when
, then that macro is expanded to produce the final
was defined. The
TABLESIZE
--and does not check to see whether it
BUFSIZE
is the result of its expansion scanned for
TABLESIZE
BUFSIZE
.
37
Chapter 3. Macros
for
#define
at some point in the source file.
that is currently
BUFSIZE
TABLESIZE
Need help?
Do you have a question about the ENTERPRISE LINUX 4 - USING CPP and is the answer not in the manual?
Questions and answers