* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx7m.html
blobdd34d0f1dfaa1c90ea34652d72f417533acf2e58
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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
21 when it stands alone.
22 Also,
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:
32 <pre> forever {
33 ...
35 </pre>which the preprocessor would expand to
36 <pre> for (;;) {
37 ...
39 </pre>which,
40 as we learned in section 3.5 on page 60,
41 is an infinite loop.
42 (Presumably there's a <TT>break</TT>; see section 3.7 p. 64.)
43 </p><p>Another popular trick is
44 <pre> #define ever ;;
45 </pre>so that you can say
46 <pre> for(ever) {
47 ...
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
57 (or expressions).
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
63 <li>no space saving
64 <li>hard to have local variables or block structure
65 <li>have to parenthesize carefully
66 (see below)
67 </UL></p><p>page 90
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
71 function-like macros:
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,
77 the invocation
78 <pre> 1 / square(n)
79 </pre>might expand to
80 <pre> 1 / n * n
81 </pre>while it should expand to
82 <pre> 1 / (n * n)
83 </pre><li>Within the macro definition,
84 all
85 occurrences
86 of the parameters
87 must be parenthesized
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,
93 the invocation
94 <pre> square(n + 1)
95 </pre>might expand to
96 <pre> n + 1 * n + 1
97 </pre>while it should expand to
98 <pre> (n + 1) * (n + 1)
99 </pre><li>If a parameter appears several times
100 in the expansion,
101 the macro may not work properly
102 if the actual argument
103 is an expression with
104 side effects.
105 No matter how we parenthesize the <TT>square()</TT> macro,
106 the invocation
107 <pre> square(i++)
108 </pre>would result in
109 <pre> i++ * i++
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
123 restricted scope,
124 if you can remember to undefine it when you want it to go out of scope.
125 Don't worry about
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.
131 </p><hr>
133 Read sequentially:
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>
138 </p>
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>
143 </p>
144 </body>
145 </html>