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

Specifying String Literals with the # Operator
If a formal parameter in the macro definition directive’s replacement string is preceded
by a # operator, it is replaced by the corresponding argument from the macro invocation,
preceded and followed by a double-quote character (") to create a string literal. This
feature, available only with the ANSI C preprocessor, may be used to turn macro
arguments into strings. This feature is often used with the fact that HP aC++ concatenates
adjacent strings.
Example:
#include <iostream.h>
#define display(arg) cout << #arg << \n //define the macro
int main()
{
display(any string you want to use); //use the macro
}
After HP aC++ expands the macro definition in the preceding program, the following
code results:
...
main ()
{
cout << any string you want to use << \n;
}
Concatenating Tokens with the ## Operator
Use the ## operator within macros to create a single token out of two other tokens.
Usually, one of these two tokens is the actual argument for a macro-parameter. Upon
expansion of the macro, each instance of the ## operator is deleted and the tokens
preceding and following the ## are concatenated into a single token.
Example 1
The following illustrates the ## operator:
// define the macro; the ## operator
// concatenates arg1 with arg2
#define concat(arg1,arg2) arg1 ## arg2
int main()
{
int concat(fire,fly);
concat(fire,fly) = 1;
printf("%d \n",concat(fire,fly));
}
Preprocessing this program yields the following:
int main()
{
int firefly;
firefly = 1;
Overview of the Preprocessor 161