C/C++ Programmer's Guide (G06.27+, H06.03+)

Table Of Contents
Preprocessor Directives and Macros
HP C/C++ Programmer’s Guide for NonStop Systems429301-010
12-3
#define
Usage Guidelines
If you need more than one physical line to complete the definition of a macro, place
a backslash (\) at the end of all but the last line of the definition. The backslashes
cause these physical lines to be concatenated into a single logical line.
The #ifdef and #ifndef directives enable you to test whether an identifier is
currently defined as a macro name.
The #undef directive enables you to remove a macro definition so that the
identifier is no longer defined as a macro name.
Unless you delete a macro definition, it remains in effect for the remainder of the
translation unit.
Avoid invoking macros with arguments that cause side effects. When the macro
evaluates an argument more than once, it also produces the side effect more than
once, sometimes with unexpected results. For example, if you invoke the sqr
macro (defined in the second example) with argument x++:
y = sqr(x++);
the preprocessor expands it to:
y = ((x++) * (x++));
This expansion causes the side effect (increment of x) to occur twice.
Examples
1. This example defines the object-like macro EOF to represent the constant -1:
#define EOF (-1)
2. This example defines the function-like macro sqr and then uses it in an
assignment:
#define sqr(parm) ((parm) * (parm))
int main(void)
{
int a,b;
a = 12;
b = sqr(a);
}
After expanding the macro invocation and substituting the argument for the
parameter in the replacement list, the assignment to b looks like this:
b = ((a) * (a));