Function-like Macros That Take Arguments

Like any user-defined function, user-defined function-like macros can also take arguments:

#define OOPS(X) printf("Oh no, %s!", X)

With that macro, any C code like this:

OOPS("Tim");

is turned into

printf("Oh no, %s!", "Tim");

during preprocessing.

Or, consider the following, more complex example:

#define AVE(X,Y)  ((X)+(Y))/2

The AVE macro now represents the code for calculating the average of two numbers. So this C code:

printf ("The average of %.1f and %.1f is
 %.2f.\n", 20.0, 118.0,
 AVE(20.0,118.0)); ...

Get C Programming: Visual Quickstart Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.