Red Hat ENTERPRISE LINUX 3 - USING CPP Using Instructions page 20

Using cpp, the c preprocessor
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING CPP:
Table of Contents

Advertisement

16
#define min(X, Y)
x = min(a, b);
y = min(1, 2);
z = min(a + 28, *p);
(In this small example you can already see several of the dangers of macro arguments. Refer to Section
3.9 Macro Pitfalls for detailed explanations.)
Leading and trailing whitespace in each argument is dropped, and all whitespace between the tokens of
an argument is reduced to a single space. Parentheses within each argument must balance; a comma
within such parentheses does not end the argument. However, there is no requirement for square
brackets or braces to balance, and they do not prevent a comma from separating arguments. Thus,
macro (array[x = y, x + 1])
passes two arguments to
as an argument, you can write it as
x + 1]
All arguments to a macro are completely macro-expanded before they are substituted into the macro
body. After substitution, the complete text is scanned again for macros to expand, including the argu-
ments. This rule may seem strange, but it is carefully designed so you need not worry about whether
any function call is actually a macro invocation. You can run into trouble if you try to be too clever,
though. Refer to Section 3.9.6 Argument Prescan for detailed discussion.
For example,
min (min (a, b), c)

min (((a)
(b) ? (a) : (b)), (c))
and then to

((((a)
(b) ? (a) : (b)))

? (((a)
(b) ? (a) : (b)))
: (c))
(Line breaks shown here for clarity would not actually be generated.)
You can leave macro arguments empty; this is not an error to the preprocessor (but many macros will
then expand to invalid code). You cannot leave out arguments entirely; if a macro takes two arguments,
there must be exactly one comma at the top level of its argument list. Here are some silly examples
using
:
min
min(, b)
min(a, )
min(,)
min((,),)
min()
error--> macro "min" requires 2 arguments, but only 1 given
min(„)
error--> macro "min" passed 3 arguments, but takes just 2

((X)
(Y) ? (X) : (Y))
==>
x = ((a)
==>
y = ((1)
==>
z = ((a + 28)
:
macro
array[x = y
is first expanded to

(c)

==> ((
)
(b) ? (

==> ((a
)
( ) ? (a

==> ((
)
( ) ? (

==> (((,))
( ) ? ((,)) : ( ))

(b) ? (a) : (b));

(2) ? (1) : (2));

(*p) ? (a + 28) : (*p));
and
. If you want to supply
x + 1]
array[(x = y, x + 1)]
) : (b))
) : ( ))
) : ( ))
Chapter 3. Macros
array[x = y,
, which is equivalent C code.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Enterprise linux 3

Table of Contents