1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Object-like Macros (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Object-like Macros (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Object-like Macros (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Macros.html" rel="up" title="Macros">
|
---|
33 | <link href="Function_002dlike-Macros.html" rel="next" title="Function-like Macros">
|
---|
34 | <link href="Macros.html" rel="prev" title="Macros">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Object_002dlike-Macros"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Function_002dlike-Macros.html" accesskey="n" rel="next">Function-like Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Object_002dlike-Macros-1"></span><h4 class="subsection">26.5.1 Object-like Macros</h4>
|
---|
64 | <span id="index-object_002dlike-macro"></span>
|
---|
65 | <span id="index-symbolic-constants"></span>
|
---|
66 | <span id="index-manifest-constants"></span>
|
---|
67 |
|
---|
68 | <p>An <em>object-like macro</em> is a simple identifier that will be
|
---|
69 | replaced by a code fragment. It is called object-like because in most
|
---|
70 | cases the use of the macro looks like reference to a data object in
|
---|
71 | code that uses it. These macros are most commonly used to give
|
---|
72 | symbolic names to numeric constants.
|
---|
73 | </p>
|
---|
74 | <span id="index-_0023define"></span>
|
---|
75 | <p>The way to define macros with the <code>#define</code> directive.
|
---|
76 | <code>#define</code> is followed by the name of the macro and then the token
|
---|
77 | sequence it should be an abbreviation for, which is variously referred
|
---|
78 | to as the macro’s <em>body</em>, <em>expansion</em> or <em>replacement
|
---|
79 | list</em>. For example,
|
---|
80 | </p>
|
---|
81 | <div class="example">
|
---|
82 | <pre class="example">#define BUFFER_SIZE 1024
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <p>defines a macro named <code>BUFFER_SIZE</code> as an abbreviation for the
|
---|
86 | token <code>1024</code>. If somewhere after this <code>#define</code> directive
|
---|
87 | there comes a C statement of the form
|
---|
88 | </p>
|
---|
89 | <div class="example">
|
---|
90 | <pre class="example">foo = (char *) malloc (BUFFER_SIZE);
|
---|
91 | </pre></div>
|
---|
92 |
|
---|
93 | <p>then preprocessing will recognize and <em>expand</em> the macro
|
---|
94 | <code>BUFFER_SIZE</code>, so that compilation will see the tokens:
|
---|
95 | </p>
|
---|
96 | <div class="example">
|
---|
97 | <pre class="example">foo = (char *) malloc (1024);
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <p>By convention, macro names are written in upper case. Programs are
|
---|
101 | easier to read when it is possible to tell at a glance which names are
|
---|
102 | macros. Macro names that start with ‘<samp>__</samp>’ are reserved for
|
---|
103 | internal uses, and many of them are defined automatically, so don’t
|
---|
104 | define such macro names unless you really know what you’re doing.
|
---|
105 | Likewise for macro names that start with ‘<samp>_</samp>’ and an upper-case letter.
|
---|
106 | </p>
|
---|
107 | <p>The macro’s body ends at the end of the <code>#define</code> line. You may
|
---|
108 | continue the definition onto multiple lines, if necessary, using
|
---|
109 | backslash-newline. When the macro is expanded, however, it will all
|
---|
110 | come out on one line. For example,
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">#define NUMBERS 1, \
|
---|
114 | 2, \
|
---|
115 | 3
|
---|
116 | int x[] = { NUMBERS };
|
---|
117 | → int x[] = { 1, 2, 3 };
|
---|
118 | </pre></div>
|
---|
119 |
|
---|
120 | <p>The most common visible consequence of this is surprising line numbers
|
---|
121 | in error messages.
|
---|
122 | </p>
|
---|
123 | <p>There is no restriction on what can go in a macro body provided it
|
---|
124 | decomposes into valid preprocessing tokens. Parentheses need not
|
---|
125 | balance, and the body need not resemble valid C code. (If it does not,
|
---|
126 | you may get error messages from the C compiler when you use the macro.)
|
---|
127 | </p>
|
---|
128 | <p>Preprocessing scans the program sequentially. A macro definition
|
---|
129 | takes effect right after its appearance. Therefore, the following
|
---|
130 | input
|
---|
131 | </p>
|
---|
132 | <div class="example">
|
---|
133 | <pre class="example">foo = X;
|
---|
134 | #define X 4
|
---|
135 | bar = X;
|
---|
136 | </pre></div>
|
---|
137 |
|
---|
138 | <p>produces
|
---|
139 | </p>
|
---|
140 | <div class="example">
|
---|
141 | <pre class="example">foo = X;
|
---|
142 | bar = 4;
|
---|
143 | </pre></div>
|
---|
144 |
|
---|
145 | <p>When preprocessing expands a macro name, the macro’s expansion
|
---|
146 | replaces the macro invocation, then the expansion is examined for more
|
---|
147 | macros to expand. For example,
|
---|
148 | </p>
|
---|
149 | <div class="example">
|
---|
150 | <pre class="example">#define TABLESIZE BUFSIZE
|
---|
151 | #define BUFSIZE 1024
|
---|
152 | TABLESIZE
|
---|
153 | → BUFSIZE
|
---|
154 | → 1024
|
---|
155 | </pre></div>
|
---|
156 |
|
---|
157 | <p><code>TABLESIZE</code> is expanded first to produce <code>BUFSIZE</code>, then that
|
---|
158 | macro is expanded to produce the final result, <code>1024</code>.
|
---|
159 | </p>
|
---|
160 | <p>Notice that <code>BUFSIZE</code> was not defined when <code>TABLESIZE</code> was
|
---|
161 | defined. The <code>#define</code> for <code>TABLESIZE</code> uses exactly the
|
---|
162 | expansion you specify—in this case, <code>BUFSIZE</code>—and does not
|
---|
163 | check to see whether it too contains macro names. Only when you
|
---|
164 | <em>use</em> <code>TABLESIZE</code> is the result of its expansion scanned for
|
---|
165 | more macro names.
|
---|
166 | </p>
|
---|
167 | <p>This makes a difference if you change the definition of <code>BUFSIZE</code>
|
---|
168 | at some point in the source file. <code>TABLESIZE</code>, defined as shown,
|
---|
169 | will always expand using the definition of <code>BUFSIZE</code> that is
|
---|
170 | currently in effect:
|
---|
171 | </p>
|
---|
172 | <div class="example">
|
---|
173 | <pre class="example">#define BUFSIZE 1020
|
---|
174 | #define TABLESIZE BUFSIZE
|
---|
175 | #undef BUFSIZE
|
---|
176 | #define BUFSIZE 37
|
---|
177 | </pre></div>
|
---|
178 |
|
---|
179 | <p>Now <code>TABLESIZE</code> expands (in two stages) to <code>37</code>.
|
---|
180 | </p>
|
---|
181 | <p>If the expansion of a macro contains its own name, either directly or
|
---|
182 | via intermediate macros, it is not expanded again when the expansion is
|
---|
183 | examined for more macros. This prevents infinite recursion.
|
---|
184 | See <a href="Self_002dReferential-Macros.html">Self-Referential Macros</a>, for the precise details.
|
---|
185 | </p>
|
---|
186 | <hr>
|
---|
187 | <div class="header">
|
---|
188 | <p>
|
---|
189 | Next: <a href="Function_002dlike-Macros.html" accesskey="n" rel="next">Function-like Macros</a>, Up: <a href="Macros.html" accesskey="u" rel="up">Macros</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
190 | </div>
|
---|
191 |
|
---|
192 |
|
---|
193 |
|
---|
194 | </body>
|
---|
195 | </html>
|
---|