#define identifier(
The macro name is identifier. The formal parameters are provided by the
The formal parameters to the macro are separated with commas. They may or may not appear in the
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
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
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