O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required