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>Self-Referential Macros (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Self-Referential Macros (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Self-Referential 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="Macro-Pitfalls.html" rel="up" title="Macro Pitfalls">
|
---|
33 | <link href="Argument-Prescan.html" rel="next" title="Argument Prescan">
|
---|
34 | <link href="Macros-and-Auto-Type.html" rel="prev" title="Macros and Auto Type">
|
---|
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="Self_002dReferential-Macros"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Argument-Prescan.html" accesskey="n" rel="next">Argument Prescan</a>, Previous: <a href="Macros-and-Auto-Type.html" accesskey="p" rel="prev">Macros and Auto Type</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</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="Self_002dReferential-Macros-1"></span><h4 class="subsubsection">26.5.10.6 Self-Referential Macros</h4>
|
---|
64 | <span id="index-self_002dreference"></span>
|
---|
65 |
|
---|
66 | <p>A <em>self-referential</em> macro is one whose name appears in its
|
---|
67 | definition. Recall that all macro definitions are rescanned for more
|
---|
68 | macros to replace. If the self-reference were considered a use of the
|
---|
69 | macro, it would produce an infinitely large expansion. To prevent
|
---|
70 | this, the self-reference is not considered a macro call: preprocessing
|
---|
71 | leaves it unchanged. Consider an example:
|
---|
72 | </p>
|
---|
73 | <div class="example">
|
---|
74 | <pre class="example">#define foo (4 + foo)
|
---|
75 | </pre></div>
|
---|
76 |
|
---|
77 | <p>where <code>foo</code> is also a variable in your program.
|
---|
78 | </p>
|
---|
79 | <p>Following the ordinary rules, each reference to <code>foo</code> will expand
|
---|
80 | into <code>(4 + foo)</code>; then this will be rescanned and will expand into
|
---|
81 | <code>(4 + (4 + foo))</code>; and so on until the computer runs out of memory.
|
---|
82 | </p>
|
---|
83 | <p>The self-reference rule cuts this process short after one step, at
|
---|
84 | <code>(4 + foo)</code>. Therefore, this macro definition has the possibly
|
---|
85 | useful effect of causing the program to add 4 to the value of <code>foo</code>
|
---|
86 | wherever <code>foo</code> is referred to.
|
---|
87 | </p>
|
---|
88 | <p>In most cases, it is a bad idea to take advantage of this feature. A
|
---|
89 | person reading the program who sees that <code>foo</code> is a variable will
|
---|
90 | not expect that it is a macro as well. The reader will come across the
|
---|
91 | identifier <code>foo</code> in the program and think its value should be that
|
---|
92 | of the variable <code>foo</code>, whereas in fact the value is four greater.
|
---|
93 | </p>
|
---|
94 | <p>It is useful to make a macro definition that expands to the macro
|
---|
95 | name itself. If you write
|
---|
96 | </p>
|
---|
97 | <div class="example">
|
---|
98 | <pre class="example">#define EPERM EPERM
|
---|
99 | </pre></div>
|
---|
100 |
|
---|
101 | <p>then the macro <code>EPERM</code> expands to <code>EPERM</code>. Effectively,
|
---|
102 | preprocessing leaves it unchanged in the source code. You can tell
|
---|
103 | that it’s a macro with <code>#ifdef</code>. You might do this if you want
|
---|
104 | to define numeric constants with an <code>enum</code>, but have
|
---|
105 | <code>#ifdef</code> be true for each constant.
|
---|
106 | </p>
|
---|
107 | <p>If a macro <code>x</code> expands to use a macro <code>y</code>, and the expansion of
|
---|
108 | <code>y</code> refers to the macro <code>x</code>, that is an <em>indirect
|
---|
109 | self-reference</em> of <code>x</code>. <code>x</code> is not expanded in this case
|
---|
110 | either. Thus, if we have
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">#define x (4 + y)
|
---|
114 | #define y (2 * x)
|
---|
115 | </pre></div>
|
---|
116 |
|
---|
117 | <p>then <code>x</code> and <code>y</code> expand as follows:
|
---|
118 | </p>
|
---|
119 | <div class="example">
|
---|
120 | <pre class="example">x → (4 + y)
|
---|
121 | → (4 + (2 * x))
|
---|
122 |
|
---|
123 | y → (2 * x)
|
---|
124 | → (2 * (4 + y))
|
---|
125 | </pre></div>
|
---|
126 |
|
---|
127 | <p>Each macro is expanded when it appears in the definition of the other
|
---|
128 | macro, but not when it indirectly appears in its own definition.
|
---|
129 | </p>
|
---|
130 | <hr>
|
---|
131 | <div class="header">
|
---|
132 | <p>
|
---|
133 | Next: <a href="Argument-Prescan.html" accesskey="n" rel="next">Argument Prescan</a>, Previous: <a href="Macros-and-Auto-Type.html" accesskey="p" rel="prev">Macros and Auto Type</a>, Up: <a href="Macro-Pitfalls.html" accesskey="u" rel="up">Macro Pitfalls</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>
|
---|
134 | </div>
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 | </body>
|
---|
139 | </html>
|
---|