您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

180 行
8.1KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1987-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation. A copy of
  7. the license is included in the
  8. section entitled "GNU Free Documentation License".
  9. This manual contains no Invariant Sections. The Front-Cover Texts are
  10. (a) (see below), and the Back-Cover Texts are (b) (see below).
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Argument Prescan (The C Preprocessor)</title>
  21. <meta name="description" content="Argument Prescan (The C Preprocessor)">
  22. <meta name="keywords" content="Argument Prescan (The C Preprocessor)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Index-of-Directives.html#Index-of-Directives" rel="index" title="Index of Directives">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Macro-Pitfalls.html#Macro-Pitfalls" rel="up" title="Macro Pitfalls">
  30. <link href="Newlines-in-Arguments.html#Newlines-in-Arguments" rel="next" title="Newlines in Arguments">
  31. <link href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" rel="prev" title="Self-Referential Macros">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Argument-Prescan"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Newlines-in-Arguments.html#Newlines-in-Arguments" accesskey="n" rel="next">Newlines in Arguments</a>, Previous: <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html#Macro-Pitfalls" accesskey="u" rel="up">Macro Pitfalls</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="Argument-Prescan-1"></a>
  68. <h4 class="subsection">3.10.6 Argument Prescan</h4>
  69. <a name="index-expansion-of-arguments"></a>
  70. <a name="index-macro-argument-expansion"></a>
  71. <a name="index-prescan-of-macro-arguments"></a>
  72. <p>Macro arguments are completely macro-expanded before they are
  73. substituted into a macro body, unless they are stringized or pasted
  74. with other tokens. After substitution, the entire macro body, including
  75. the substituted arguments, is scanned again for macros to be expanded.
  76. The result is that the arguments are scanned <em>twice</em> to expand
  77. macro calls in them.
  78. </p>
  79. <p>Most of the time, this has no effect. If the argument contained any
  80. macro calls, they are expanded during the first scan. The result
  81. therefore contains no macro calls, so the second scan does not change
  82. it. If the argument were substituted as given, with no prescan, the
  83. single remaining scan would find the same macro calls and produce the
  84. same results.
  85. </p>
  86. <p>You might expect the double scan to change the results when a
  87. self-referential macro is used in an argument of another macro
  88. (see <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros">Self-Referential Macros</a>): the self-referential macro would be
  89. expanded once in the first scan, and a second time in the second scan.
  90. However, this is not what happens. The self-references that do not
  91. expand in the first scan are marked so that they will not expand in the
  92. second scan either.
  93. </p>
  94. <p>You might wonder, &ldquo;Why mention the prescan, if it makes no difference?
  95. And why not skip it and make the preprocessor faster?&rdquo; The answer is
  96. that the prescan does make a difference in three special cases:
  97. </p>
  98. <ul>
  99. <li> Nested calls to a macro.
  100. <p>We say that <em>nested</em> calls to a macro occur when a macro&rsquo;s argument
  101. contains a call to that very macro. For example, if <code>f</code> is a macro
  102. that expects one argument, <code>f (f (1))</code> is a nested pair of calls to
  103. <code>f</code>. The desired expansion is made by expanding <code>f (1)</code> and
  104. substituting that into the definition of <code>f</code>. The prescan causes
  105. the expected result to happen. Without the prescan, <code>f (1)</code> itself
  106. would be substituted as an argument, and the inner use of <code>f</code> would
  107. appear during the main scan as an indirect self-reference and would not
  108. be expanded.
  109. </p>
  110. </li><li> Macros that call other macros that stringize or concatenate.
  111. <p>If an argument is stringized or concatenated, the prescan does not
  112. occur. If you <em>want</em> to expand a macro, then stringize or
  113. concatenate its expansion, you can do that by causing one macro to call
  114. another macro that does the stringizing or concatenation. For
  115. instance, if you have
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample">#define AFTERX(x) X_ ## x
  119. #define XAFTERX(x) AFTERX(x)
  120. #define TABLESIZE 1024
  121. #define BUFSIZE TABLESIZE
  122. </pre></div>
  123. <p>then <code>AFTERX(BUFSIZE)</code> expands to <code>X_BUFSIZE</code>, and
  124. <code>XAFTERX(BUFSIZE)</code> expands to <code>X_1024</code>. (Not to
  125. <code>X_TABLESIZE</code>. Prescan always does a complete expansion.)
  126. </p>
  127. </li><li> Macros used in arguments, whose expansions contain unshielded commas.
  128. <p>This can cause a macro expanded on the second scan to be called with the
  129. wrong number of arguments. Here is an example:
  130. </p>
  131. <div class="smallexample">
  132. <pre class="smallexample">#define foo a,b
  133. #define bar(x) lose(x)
  134. #define lose(x) (1 + (x))
  135. </pre></div>
  136. <p>We would like <code>bar(foo)</code> to turn into <code>(1 + (foo))</code>, which
  137. would then turn into <code>(1 + (a,b))</code>. Instead, <code>bar(foo)</code>
  138. expands into <code>lose(a,b)</code>, and you get an error because <code>lose</code>
  139. requires a single argument. In this case, the problem is easily solved
  140. by the same parentheses that ought to be used to prevent misnesting of
  141. arithmetic operations:
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">#define foo (a,b)
  145. </pre><pre class="smallexample">or
  146. </pre><pre class="smallexample">#define bar(x) lose((x))
  147. </pre></div>
  148. <p>The extra pair of parentheses prevents the comma in <code>foo</code>&rsquo;s
  149. definition from being interpreted as an argument separator.
  150. </p>
  151. </li></ul>
  152. <hr>
  153. <div class="header">
  154. <p>
  155. Next: <a href="Newlines-in-Arguments.html#Newlines-in-Arguments" accesskey="n" rel="next">Newlines in Arguments</a>, Previous: <a href="Self_002dReferential-Macros.html#Self_002dReferential-Macros" accesskey="p" rel="prev">Self-Referential Macros</a>, Up: <a href="Macro-Pitfalls.html#Macro-Pitfalls" accesskey="u" rel="up">Macro Pitfalls</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Index-of-Directives.html#Index-of-Directives" title="Index" rel="index">Index</a>]</p>
  156. </div>
  157. </body>
  158. </html>