source: public/doc/gnu-c/Self_002dReferential-Macros.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.1 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
67definition. Recall that all macro definitions are rescanned for more
68macros to replace. If the self-reference were considered a use of the
69macro, it would produce an infinitely large expansion. To prevent
70this, the self-reference is not considered a macro call: preprocessing
71leaves 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
80into <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
85useful effect of causing the program to add 4 to the value of <code>foo</code>
86wherever <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
89person reading the program who sees that <code>foo</code> is a variable will
90not expect that it is a macro as well. The reader will come across the
91identifier <code>foo</code> in the program and think its value should be that
92of 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
95name 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,
102preprocessing leaves it unchanged in the source code. You can tell
103that it&rsquo;s a macro with <code>#ifdef</code>. You might do this if you want
104to 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
109self-reference</em> of <code>x</code>. <code>x</code> is not expanded in this case
110either. 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 &rarr; (4 + y)
121 &rarr; (4 + (2 * x))
122
123y &rarr; (2 * x)
124 &rarr; (2 * (4 + y))
125</pre></div>
126
127<p>Each macro is expanded when it appears in the definition of the other
128macro, but not when it indirectly appears in its own definition.
129</p>
130<hr>
131<div class="header">
132<p>
133Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.