Let's take a macro to calculate the maximum of two values:
#define MAX(a, b) ((a) < (b) ? (a) : (b))
Then we use it like this:
int x = 5;
int y = 10;
int max = MAX(x++, y++);
Then the macro is expanded to
int max = ((x++) < (y++) ? (x++) : (y++));
As you can see, the increment operation on either x
or y
will happen twice, not what would happen if you had a function where each argument you pass is evaluated only once.
Another important point is the use of parentheses in the macro. Let's take another simple macro:
#define MUL(a, b) a * b
Now if you invoke the macro as
int sum = MUL(x + 3, y - 2);
then the expansion becomes
int sum = x + 3 * y - 2;
Which due to operator precedence is equal to
int sum = x + (3 * y) - 2;
Often not quite what was expected, if one expects (x + 3) * (y - 2)
.
This problem is also "solved" by using functions.