Compile and run this program:
Code:
#include <stdio.h>
#define getmax_v1(x, y) ((x) > (y) ? (x) : (y))
#define getmax_v2(x, y) (x > y) ? x : y
int main(void)
{
int a = 10;
int b = 20;
printf("v1: %d\n", getmax_v1(a, b + 1) * 2);
printf("v2: %d\n", getmax_v2(a, b + 1) * 2);
return 0;
}
In general, to avoid these kinds of problems, unless the expression is already a primary expression (e.g., a constant), we surround the entire expression in a pair of parentheses, and surround each of the macro identifiers in the expression with parentheses as they could be complex expressions, so this eliminates the chance that precedence rules might cause an unexpected result.