#define identifier( [identifier-list] ) [replacement-list]

The macro name is identifier. The formal parameters are provided by the identifier-listenclosed in parentheses. The open parenthesis ( ‘(’ ) must immediately follow the identifier with no intervening white space. If there is a space between the identifier and the parenthesis, the macro is defined as if it were the first form and the replacement-list begins with the ( character.

The formal parameters to the macro are separated with commas. They may or may not appear in the replacement-list. When the macro is invoked, the actual arguments are placed in a parenthesized list following the macro name. Commas enclosed in additional matching pairs of parentheses do not separate arguments but are themselves components of arguments.

The actual arguments replace the formal parameters in the token string when the macro is invoked.

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; printf("%d \n",firefly );

}

122 Preprocessing Directives

Page 122
Image 122
HP C/aC++ for PA-RISC Software Specifying String Literals with the # Operator, Concatenating Tokens with the ## Operator