1<html lang="en"> 2<head> 3<title>Self-Referential Macros - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Macro-Pitfalls.html#Macro-Pitfalls" title="Macro Pitfalls"> 9<link rel="prev" href="Duplication-of-Side-Effects.html#Duplication-of-Side-Effects" title="Duplication of Side Effects"> 10<link rel="next" href="Argument-Prescan.html#Argument-Prescan" title="Argument Prescan"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 141997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 152008, 2009, 2010, 2011 16Free Software Foundation, Inc. 17 18Permission is granted to copy, distribute and/or modify this document 19under the terms of the GNU Free Documentation License, Version 1.3 or 20any later version published by the Free Software Foundation. A copy of 21the license is included in the 22section entitled ``GNU Free Documentation License''. 23 24This manual contains no Invariant Sections. The Front-Cover Texts are 25(a) (see below), and the Back-Cover Texts are (b) (see below). 26 27(a) The FSF's Front-Cover Text is: 28 29 A GNU Manual 30 31(b) The FSF's Back-Cover Text is: 32 33 You have freedom to copy and modify this GNU Manual, like GNU 34 software. Copies published by the Free Software Foundation raise 35 funds for GNU development. 36--> 37<meta http-equiv="Content-Style-Type" content="text/css"> 38<style type="text/css"><!-- 39 pre.display { font-family:inherit } 40 pre.format { font-family:inherit } 41 pre.smalldisplay { font-family:inherit; font-size:smaller } 42 pre.smallformat { font-family:inherit; font-size:smaller } 43 pre.smallexample { font-size:smaller } 44 pre.smalllisp { font-size:smaller } 45 span.sc { font-variant:small-caps } 46 span.roman { font-family:serif; font-weight:normal; } 47 span.sansserif { font-family:sans-serif; font-weight:normal; } 48--></style> 49<link rel="stylesheet" type="text/css" href="../cs.css"> 50</head> 51<body> 52<div class="node"> 53<a name="Self-Referential-Macros"></a> 54<a name="Self_002dReferential-Macros"></a> 55<p> 56Next: <a rel="next" accesskey="n" href="Argument-Prescan.html#Argument-Prescan">Argument Prescan</a>, 57Previous: <a rel="previous" accesskey="p" href="Duplication-of-Side-Effects.html#Duplication-of-Side-Effects">Duplication of Side Effects</a>, 58Up: <a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a> 59<hr> 60</div> 61 62<h4 class="subsection">3.10.5 Self-Referential Macros</h4> 63 64<p><a name="index-self_002dreference-78"></a> 65A <dfn>self-referential</dfn> macro is one whose name appears in its 66definition. Recall that all macro definitions are rescanned for more 67macros to replace. If the self-reference were considered a use of the 68macro, it would produce an infinitely large expansion. To prevent this, 69the self-reference is not considered a macro call. It is passed into 70the preprocessor output unchanged. Consider an example: 71 72<pre class="smallexample"> #define foo (4 + foo) 73</pre> 74 <p class="noindent">where <code>foo</code> is also a variable in your program. 75 76 <p>Following the ordinary rules, each reference to <code>foo</code> will expand 77into <code>(4 + foo)</code>; then this will be rescanned and will expand into 78<code>(4 + (4 + foo))</code>; and so on until the computer runs out of memory. 79 80 <p>The self-reference rule cuts this process short after one step, at 81<code>(4 + foo)</code>. Therefore, this macro definition has the possibly 82useful effect of causing the program to add 4 to the value of <code>foo</code> 83wherever <code>foo</code> is referred to. 84 85 <p>In most cases, it is a bad idea to take advantage of this feature. A 86person reading the program who sees that <code>foo</code> is a variable will 87not expect that it is a macro as well. The reader will come across the 88identifier <code>foo</code> in the program and think its value should be that 89of the variable <code>foo</code>, whereas in fact the value is four greater. 90 91 <p>One common, useful use of self-reference is to create a macro which 92expands to itself. If you write 93 94<pre class="smallexample"> #define EPERM EPERM 95</pre> 96 <p class="noindent">then the macro <code>EPERM</code> expands to <code>EPERM</code>. Effectively, it is 97left alone by the preprocessor whenever it's used in running text. You 98can tell that it's a macro with ‘<samp><span class="samp">#ifdef</span></samp>’. You might do this if you 99want to define numeric constants with an <code>enum</code>, but have 100‘<samp><span class="samp">#ifdef</span></samp>’ be true for each constant. 101 102 <p>If a macro <code>x</code> expands to use a macro <code>y</code>, and the expansion of 103<code>y</code> refers to the macro <code>x</code>, that is an <dfn>indirect 104self-reference</dfn> of <code>x</code>. <code>x</code> is not expanded in this case 105either. Thus, if we have 106 107<pre class="smallexample"> #define x (4 + y) 108 #define y (2 * x) 109</pre> 110 <p class="noindent">then <code>x</code> and <code>y</code> expand as follows: 111 112<pre class="smallexample"> x ==> (4 + y) 113 ==> (4 + (2 * x)) 114 115 y ==> (2 * x) 116 ==> (2 * (4 + y)) 117</pre> 118 <p class="noindent">Each macro is expanded when it appears in the definition of the other 119macro, but not when it indirectly appears in its own definition. 120 121 </body></html> 122 123