HP aC++/HP C A.06.25 Programmer's Guide

NOTE: The result of the preprocessor concatenation operator ## must be a _single_
token. In particular, the use of ## to concatenate strings is redundant and not legal C
or C++. For example:
#include
#define concat_token(a, b) a##b
#define concat_string(a, b) a b
int main() {
// Wrong:
printf("%s\n", concat_token("Hello,", " World!"));
// Correct:
printf("%s\n", concat_string("Hello,", " World!"));
// Best: (macro not needed at all!):
printf("%s\n", "Hello," " World!");
}
Using Macros to Define Constants
The most common use of the macro replacement is in defining a constant. In C++ you
can also declare constants using the keyword const. Rather than explicitly putting
constant values in a program, you can name the constants using macros, then use the
names in place of the constants. By changing the definition of the macro, you can more
easily change the program:
#define ARRAY_SIZE 1000float x[ARRAY_SIZE];
In this example, the array x is dimensioned using the macro ARRAY_SIZE rather than
the constant 1000. Note that expressions that may use the array can also use the macro
instead of the actual constant:
for (i=0; i<<ARRAY_SIZE; ++i) f+=x[i];
Changing the dimension of x means only changing the macro for ARRAY_SIZE. The
dimension changes and so do all of the expressions that make use of the dimension.
Other Macros
Some other common macros used by C programmers include:
#define FALSE 0
#define TRUE 1
The following macro is more complex. It has two parameters and produces an inline
expression which is equal to the maximum of its two parameters:
#define MAX(x,y) ((x) > (y) ? (x) : (y))
Overview of the Preprocessor 163