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. -->
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
>
15 <H2>9.2 Macro Definition and Substitution
</H2>
17 <p>[This section corresponds to K
&R Sec.
4.11.2]
18 </p><p>A preprocessor line of the form
20 #define
<I>name
</I> <I>text
</I>
22 defines a
<dfn>macro
</dfn> with the given name,
23 having as its
<dfn>value
</dfn> the given replacement text.
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
33 it is customary to give them names which are all capital letters
34 (or at least which begin with a capital letter).
38 not restricted to numbers,
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
50 is neither readable nor reliable;
51 it's not necessarily obvious
52 what all those
100's scattered around the program are,
54 if we ever decide that
100 is too small for the size of the
56 we'll have to remember to change the number in two
59 A much better solution is to use a macro:
64 getline(line, MAXLINE);
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,
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
83 (this is a pretty meaningless example,
84 but the situation does come up in practice).
86 suppose that you write
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>,
100 Then it substitutes the macros
<TT>A
</TT> and
<TT>B
</TT>,
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
117 then the declaration of
<TT>x
</TT> would
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
133 #define MAXLINE
100; /* WRONG */
135 then when you later declare
139 the preprocessor will expand it to
141 char line[
100;]; /* WRONG */
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
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.
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>
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>