Example 1 — SQUARE, MAX, and SHOW
The parenthesized form gives correct results for expressions.
#include <stdio.h>
#define SQUARE(x) ((x) * (x))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define SHOW(x) printf(#x " = %d\n", (x))
int main(void) {
int n = 7;
SHOW(SQUARE(3 + 1));
SHOW(MAX(4, 9));
SHOW(n * 2);
return 0;
} How It Works
SQUARE(3 + 1)is((3 + 1) * (3 + 1))= 16.#xconverts the argument text into a string literal.- Adjacent string literals are joined, so
#x " = %d\n"becomes one format string.
