The C Preprocessor - Cascaded Macros

Node: Cascaded Macros Next: Newlines in Args Prev: Argument Prescan Up: Macro Pitfalls

Cascaded Use of Macros

A cascade of macros is when one macro's body contains a reference to another macro. This is very common practice. For example,

	#define BUFSIZE 1020
	#define TABLESIZE BUFSIZE

This is not at all the same as defining `TABLESIZE' to be `1020'. The `#define' for `TABLESIZE' uses exactly the body you specify---in this case, `BUFSIZE'---and does not check to see whether it too is the name of a macro.

It's only when you use `TABLESIZE' that the result of its expansion is checked for more macro names.

This makes a difference if you change the definition of `BUFSIZE' at some point in the source file. `TABLESIZE', defined as shown, will always expand using the definition of `BUFSIZE' that is currently in effect:

	#define BUFSIZE 1020
	#define TABLESIZE BUFSIZE
	#undef BUFSIZE
	#define BUFSIZE 37

Now `TABLESIZE' expands (in two stages) to `37'. (The `#undef' is to prevent any warning about the nontrivial redefinition of BUFSIZE .)


Next: Newlines in Args Up: Macro Pitfalls