* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx9b.html
blob9626706b5e3abd6f6a9732377ced4add01225ef1
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>9.2 Macro Definition and Substitution</title>
10 <link href="sx9a.html" rev=precedes>
11 <link href="sx9c.html" rel=precedes>
12 <link href="sx9.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>9.2 Macro Definition and Substitution</H2>
17 <p>[This section corresponds to K&amp;R Sec. 4.11.2]
18 </p><p>A preprocessor line of the form
19 <pre>
20 #define <I>name</I> <I>text</I>
21 </pre>
22 defines a <dfn>macro</dfn> with the given name,
23 having as its <dfn>value</dfn> the given replacement text.
24 After that
25 (for the rest of the current source file),
26 wherever the preprocessor sees that name,
27 it will replace it with the replacement text.
28 The name follows the same rules as ordinary identifiers
29 (it can contain only letters, digits, and underscores,
30 and may not begin with a digit).
31 Since macros behave quite differently from normal variables (or
32 functions),
33 it is customary to give them names which are all capital letters
34 (or at least which begin with a capital letter).
35 The replacement text
36 can be absolutely
37 anything--it's
38 not restricted to numbers,
39 or simple strings,
40 or anything.
41 </p><p>The most common use for macros is to propagate various
42 constants around and to make them more self-documenting.
43 We've been saying things like
44 <pre>
45 char line[100];
46 ...
47 getline(line, 100);
48 </pre>
49 but this
50 is neither readable nor reliable;
51 it's not necessarily obvious
52 what all those 100's scattered around the program are,
53 and
54 if we ever decide that 100 is too small for the size of the
55 array to hold lines,
56 we'll have to remember to change the number in two
57 (or more)
58 places.
59 A much better solution is to use a macro:
60 <pre>
61 #define MAXLINE 100
62 char line[MAXLINE];
63 ...
64 getline(line, MAXLINE);
65 </pre>
66 Now,
67 if we ever want to change the size, we only have to do it in one place,
68 and it's more obvious what the words <TT>MAXLINE</TT> sprinkled
69 through the program mean than the magic numbers 100 did.
71 </p><p>Since the replacement text of a preprocessor macro can be anything,
72 it can also be an expression,
73 although you have to realize that,
74 as always,
75 the text is substituted (and perhaps evaluated) later.
76 No evaluation is performed when the macro is defined.
77 For example, suppose that you write something like
78 <pre>
79 #define A 2
80 #define B 3
81 #define C A + B
82 </pre>
83 (this is a pretty meaningless example,
84 but the situation does come up in practice).
85 Then, later,
86 suppose that you write
87 <pre>
88 int x = C * 2;
89 </pre>
90 If <TT>A</TT>, <TT>B</TT>, and <TT>C</TT> were ordinary variables,
91 you'd expect <TT>x</TT> to end up with the value 10.
92 But let's see what happens.
93 </p><p>The preprocessor always substitutes text for macros
94 exactly as you have written it.
95 So it first substitites the replacement text for the macro <TT>C</TT>,
96 resulting in
97 <pre>
98 int x = A + B * 2;
99 </pre>
100 Then it substitutes the macros <TT>A</TT> and <TT>B</TT>,
101 resulting in
102 <pre>
103 int x = 2 + 3 * 2;
104 </pre>
105 Only when the preprocessor is done doing all this substituting
106 does the compiler get into the act.
107 But when it evaluates that expression
108 (using the normal precedence of multiplication over addition),
109 it ends up initializing <TT>x</TT> with the value 8!
110 </p><p>To guard against this sort of problem,
111 it is always a good idea to include explicit parentheses
112 in the definitions of macros which contain expressions.
113 If we were to define the macro <TT>C</TT> as
114 <pre>
115 #define C (A + B)
116 </pre>
117 then the declaration of <TT>x</TT> would
118 ultimately
119 expand to
120 <pre>
121 int x = (2 + 3) * 2;
122 </pre>
123 and <TT>x</TT> would be initialized to 10,
124 as we probably expected.
125 </p><p>Notice that there does not have to be
126 (and in fact there usually is <em>not</em>)
127 a semicolon at the end of
128 a <TT>#define</TT> line.
129 (This is just one of the ways that the syntax of the
130 preprocessor is different from the rest of C.)
131 If you accidentally type
132 <pre>
133 #define MAXLINE 100; /* WRONG */
134 </pre>
135 then when you later declare
136 <pre>
137 char line[MAXLINE];
138 </pre>
139 the preprocessor will expand it to
140 <pre>
141 char line[100;]; /* WRONG */
142 </pre>
143 which is a syntax error.
145 This is what we mean when we say that the preprocessor doesn't
146 know much of anything about the syntax of C--in this last example,
147 the <dfn>value</dfn> or replacement text for the macro
148 <TT>MAXLINE</TT> was the 4 characters
149 <TT>1 0 0 ; </TT>, and that's exactly what the
150 preprocessor substituted
151 (even though it didn't make any sense).
152 </p><p>Simple macros like <TT>MAXLINE</TT> act sort of like little variables,
153 whose values are constant
154 (or constant expressions).
155 It's also possible to have macros which look like little functions
156 (that is,
157 you invoke them with what looks like function call syntax,
158 and they expand to replacement text which is a function
159 of the actual arguments they are invoked with)
160 but we won't be looking at these yet.
162 </p><hr>
164 Read sequentially:
165 <a href="sx9a.html" rev=precedes>prev</a>
166 <a href="sx9c.html" rel=precedes>next</a>
167 <a href="sx9.html" rev=subdocument>up</a>
168 <a href="top.html">top</a>
169 </p>
171 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
172 // <a href="copyright.html">Copyright</a> 1995-1997
173 // <a href="mailto:scs@eskimo.com">mail feedback</a>
174 </p>
175 </body>
176 </html>