Using Arguments with #define
By using arguments, you can create function-like macros that look and act much like functions. A macro with arguments looks very similar to a function because the arguments are enclosed within parentheses. Function-like macro definitions have one or more arguments in parentheses, and these arguments then appear in the replacement portion, as shown in Figure 16.2.
Figure 16.2. Parts of a function-like macro definition.
Here's a sample definition:
#define SQUARE(X) X *X
It can be used in program like this:
z = SQUARE(2);
This looks like a function call, but it doesn't necessarily behave identically. Listing 16.2 ...
Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.