1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>section
4.11.2: Macro Substitution
</title>
10 <link href=
"sx7l.html" rev=precedes
>
11 <link href=
"sx7n.html" rel=precedes
>
12 <link href=
"sx7.html" rev=subdocument
>
15 <H2>section
4.11.2: Macro Substitution
</H2>
17 <p><TT>#define
</TT>s last for the whole file;
18 you can't have local ones like you can for local variables.
19 </p><p>``Substitutions are made only for tokens''
20 means that a substitutable macro name is only recognized
23 substitution never happens in quoted strings,
24 because it turns out that you usually don't want it to.
25 Strings are generally used for communication with the user,
26 while you want substitutions to happen where you're talking to the compiler.
28 </p><p>The point of the ``
<TT>forever
</TT>'' example
29 is to demonstrate that the replacement text
30 doesn't have to be a simple number or string constant.
31 You'd use the
<TT>forever
</TT> macro like this:
35 </pre>which the preprocessor would expand to
40 as we learned in section
3.5 on page
60,
42 (Presumably there's a
<TT>break
</TT>; see section
3.7 p.
64.)
43 </p><p>Another popular trick is
45 </pre>so that you can say
49 </pre>But ``preprocessor tricks'' like these
50 tend to get out of hand very quickly;
51 if you use too many of them
52 you're not writing in C any more
53 but rather in your own peculiar dialect,
54 and no one will be able to read your code
55 without understanding all of your ``silly little macros.''
56 It
<em>is
</em> best if simple macros expand to simple constants
58 </p><p>Macros with arguments are also called ``function-like macros''
59 because they act almost like miniature functions.
60 There are some important differences, however:
61 <UL><li>no call-by-value copying semantics
64 <li>hard to have local variables or block structure
65 <li>have to parenthesize carefully
68 </p><p>The correct way to write the
<TT>square()
</TT> macro is
69 <pre> #define square(x) ((x) * (x))
70 </pre>There are three rules to remember when defining
72 </p><OL><li>The macro expansion must always be parenthesized
73 so that any low-precedence operators it contains
74 will still be evaluated first.
76 If we didn't write the
<TT>square()
</TT> macro carefully,
81 </pre>while it should expand to
83 </pre><li>Within the macro definition,
88 so that any low-precedence operators
89 the actual arguments contain
90 will be evaluated first.
92 If we didn't write the
<TT>square()
</TT> macro carefully,
97 </pre>while it should expand to
98 <pre> (n +
1) * (n +
1)
99 </pre><li>If a parameter appears several times
101 the macro may not work properly
102 if the actual argument
103 is an expression with
105 No matter how we parenthesize the
<TT>square()
</TT> macro,
108 </pre>would result in
110 </pre>(perhaps with some parentheses),
111 but this expression is undefined,
112 because we don't know when the two increments
113 will happen with respect to each other or the multiplication.
114 </OL>Since the
<TT>square()
</TT> macro can't be written perfectly safely,
115 (arguments with side effects will always be troublesome),
116 its callers will always have to be careful
117 (i.e. not to call it with arguments with side effects).
118 One convention is to capitalize the names of macros
119 which can't be treated
<em>exactly
</em> as if they were functions:
120 <pre> #define Square(x) ((x) * (x))
121 </pre><p>page
90 continued
122 </p><p><TT>#undef
</TT> can be used when you want to give a macro
124 if you can remember to undefine it when you want it to go out of scope.
126 ``[ensuring] that a routine is really a function, not a macro''
127 or the
<TT>getchar
</TT> example.
128 </p><p>Also, don't worry about the
<TT>#
</TT> and
<TT>##
</TT> operators.
129 These are new ANSI features
130 which aren't needed except in relatively special circumstances.
134 <a href=
"sx7l.html" rev=precedes
>prev
</a>
135 <a href=
"sx7n.html" rel=precedes
>next
</a>
136 <a href=
"sx7.html" rev=subdocument
>up
</a>
137 <a href=
"top.html">top
</a>
140 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
141 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
142 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>