Saturday, October 17, 2009

Defining Function-Like Macros with #define

You can specify one or more arguments to a macro name defined by the #define directive, so that the macro name can
be treated like a simple function that accepts arguments.
For instance, the following macro name, MULTIPLY, takes two arguments:
#define MULTIPLY(val1, val2) ((val1) * (val2))
When the following statement:
result = MULTIPLY(2, 3) + 10;
is preprocessed, the preprocessor substitutes the expression 2 for val1 and 3 for val2, and then produces the following
equivalent:
result = ((2) * (3)) + 10;

No comments:

Post a Comment