1<html lang="en"> 2<head> 3<title>Argument Prescan - 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="Self_002dReferential-Macros.html#Self_002dReferential-Macros" title="Self-Referential Macros"> 10<link rel="next" href="Newlines-in-Arguments.html#Newlines-in-Arguments" title="Newlines in Arguments"> 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="Argument-Prescan"></a> 54<p> 55Next: <a rel="next" accesskey="n" href="Newlines-in-Arguments.html#Newlines-in-Arguments">Newlines in Arguments</a>, 56Previous: <a rel="previous" accesskey="p" href="Self_002dReferential-Macros.html#Self_002dReferential-Macros">Self-Referential Macros</a>, 57Up: <a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a> 58<hr> 59</div> 60 61<h4 class="subsection">3.10.6 Argument Prescan</h4> 62 63<p><a name="index-expansion-of-arguments-79"></a><a name="index-macro-argument-expansion-80"></a><a name="index-prescan-of-macro-arguments-81"></a> 64Macro arguments are completely macro-expanded before they are 65substituted into a macro body, unless they are stringified or pasted 66with other tokens. After substitution, the entire macro body, including 67the substituted arguments, is scanned again for macros to be expanded. 68The result is that the arguments are scanned <em>twice</em> to expand 69macro calls in them. 70 71 <p>Most of the time, this has no effect. If the argument contained any 72macro calls, they are expanded during the first scan. The result 73therefore contains no macro calls, so the second scan does not change 74it. If the argument were substituted as given, with no prescan, the 75single remaining scan would find the same macro calls and produce the 76same results. 77 78 <p>You might expect the double scan to change the results when a 79self-referential macro is used in an argument of another macro 80(see <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros">Self-Referential Macros</a>): the self-referential macro would be 81expanded once in the first scan, and a second time in the second scan. 82However, this is not what happens. The self-references that do not 83expand in the first scan are marked so that they will not expand in the 84second scan either. 85 86 <p>You might wonder, “Why mention the prescan, if it makes no difference? 87And why not skip it and make the preprocessor faster?” The answer is 88that the prescan does make a difference in three special cases: 89 90 <ul> 91<li>Nested calls to a macro. 92 93 <p>We say that <dfn>nested</dfn> calls to a macro occur when a macro's argument 94contains a call to that very macro. For example, if <code>f</code> is a macro 95that expects one argument, <code>f (f (1))</code> is a nested pair of calls to 96<code>f</code>. The desired expansion is made by expanding <code>f (1)</code> and 97substituting that into the definition of <code>f</code>. The prescan causes 98the expected result to happen. Without the prescan, <code>f (1)</code> itself 99would be substituted as an argument, and the inner use of <code>f</code> would 100appear during the main scan as an indirect self-reference and would not 101be expanded. 102 103 <li>Macros that call other macros that stringify or concatenate. 104 105 <p>If an argument is stringified or concatenated, the prescan does not 106occur. If you <em>want</em> to expand a macro, then stringify or 107concatenate its expansion, you can do that by causing one macro to call 108another macro that does the stringification or concatenation. For 109instance, if you have 110 111 <pre class="smallexample"> #define AFTERX(x) X_ ## x 112 #define XAFTERX(x) AFTERX(x) 113 #define TABLESIZE 1024 114 #define BUFSIZE TABLESIZE 115</pre> 116 <p>then <code>AFTERX(BUFSIZE)</code> expands to <code>X_BUFSIZE</code>, and 117<code>XAFTERX(BUFSIZE)</code> expands to <code>X_1024</code>. (Not to 118<code>X_TABLESIZE</code>. Prescan always does a complete expansion.) 119 120 <li>Macros used in arguments, whose expansions contain unshielded commas. 121 122 <p>This can cause a macro expanded on the second scan to be called with the 123wrong number of arguments. Here is an example: 124 125 <pre class="smallexample"> #define foo a,b 126 #define bar(x) lose(x) 127 #define lose(x) (1 + (x)) 128</pre> 129 <p>We would like <code>bar(foo)</code> to turn into <code>(1 + (foo))</code>, which 130would then turn into <code>(1 + (a,b))</code>. Instead, <code>bar(foo)</code> 131expands into <code>lose(a,b)</code>, and you get an error because <code>lose</code> 132requires a single argument. In this case, the problem is easily solved 133by the same parentheses that ought to be used to prevent misnesting of 134arithmetic operations: 135 136 <pre class="smallexample"> #define foo (a,b) 137 <br>or<br> 138 #define bar(x) lose((x)) 139</pre> 140 <p>The extra pair of parentheses prevents the comma in <code>foo</code>'s 141definition from being interpreted as an argument separator. 142 143 </ul> 144 145 </body></html> 146 147