Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

180 lines
8.2KB

  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>Traditional macros (The C Preprocessor)</title>
  21. <meta name="description" content="Traditional macros (The C Preprocessor)">
  22. <meta name="keywords" content="Traditional macros (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="Traditional-Mode.html#Traditional-Mode" rel="up" title="Traditional Mode">
  30. <link href="Traditional-miscellany.html#Traditional-miscellany" rel="next" title="Traditional miscellany">
  31. <link href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" rel="prev" title="Traditional lexical analysis">
  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="Traditional-macros"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Traditional-miscellany.html#Traditional-miscellany" accesskey="n" rel="next">Traditional miscellany</a>, Previous: <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" accesskey="p" rel="prev">Traditional lexical analysis</a>, Up: <a href="Traditional-Mode.html#Traditional-Mode" accesskey="u" rel="up">Traditional Mode</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="Traditional-macros-1"></a>
  68. <h3 class="section">10.2 Traditional macros</h3>
  69. <p>The major difference between traditional and ISO macros is that the
  70. former expand to text rather than to a token sequence. CPP removes
  71. all leading and trailing horizontal whitespace from a macro&rsquo;s
  72. replacement text before storing it, but preserves the form of internal
  73. whitespace.
  74. </p>
  75. <p>One consequence is that it is legitimate for the replacement text to
  76. contain an unmatched quote (see <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis">Traditional lexical analysis</a>). An
  77. unclosed string or character constant continues into the text
  78. following the macro call. Similarly, the text at the end of a macro&rsquo;s
  79. expansion can run together with the text after the macro invocation to
  80. produce a single token.
  81. </p>
  82. <p>Normally comments are removed from the replacement text after the
  83. macro is expanded, but if the <samp>-CC</samp> option is passed on the
  84. command-line comments are preserved. (In fact, the current
  85. implementation removes comments even before saving the macro
  86. replacement text, but it careful to do it in such a way that the
  87. observed effect is identical even in the function-like macro case.)
  88. </p>
  89. <p>The ISO stringizing operator &lsquo;<samp>#</samp>&rsquo; and token paste operator
  90. &lsquo;<samp>##</samp>&rsquo; have no special meaning. As explained later, an effect
  91. similar to these operators can be obtained in a different way. Macro
  92. names that are embedded in quotes, either from the main file or after
  93. macro replacement, do not expand.
  94. </p>
  95. <p>CPP replaces an unquoted object-like macro name with its replacement
  96. text, and then rescans it for further macros to replace. Unlike
  97. standard macro expansion, traditional macro expansion has no provision
  98. to prevent recursion. If an object-like macro appears unquoted in its
  99. replacement text, it will be replaced again during the rescan pass,
  100. and so on <em>ad infinitum</em>. GCC detects when it is expanding
  101. recursive macros, emits an error message, and continues after the
  102. offending macro invocation.
  103. </p>
  104. <div class="smallexample">
  105. <pre class="smallexample">#define PLUS +
  106. #define INC(x) PLUS+x
  107. INC(foo);
  108. &rarr; ++foo;
  109. </pre></div>
  110. <p>Function-like macros are similar in form but quite different in
  111. behavior to their ISO counterparts. Their arguments are contained
  112. within parentheses, are comma-separated, and can cross physical lines.
  113. Commas within nested parentheses are not treated as argument
  114. separators. Similarly, a quote in an argument cannot be left
  115. unclosed; a following comma or parenthesis that comes before the
  116. closing quote is treated like any other character. There is no
  117. facility for handling variadic macros.
  118. </p>
  119. <p>This implementation removes all comments from macro arguments, unless
  120. the <samp>-C</samp> option is given. The form of all other horizontal
  121. whitespace in arguments is preserved, including leading and trailing
  122. whitespace. In particular
  123. </p>
  124. <div class="smallexample">
  125. <pre class="smallexample">f( )
  126. </pre></div>
  127. <p>is treated as an invocation of the macro &lsquo;<samp>f</samp>&rsquo; with a single
  128. argument consisting of a single space. If you want to invoke a
  129. function-like macro that takes no arguments, you must not leave any
  130. whitespace between the parentheses.
  131. </p>
  132. <p>If a macro argument crosses a new line, the new line is replaced with
  133. a space when forming the argument. If the previous line contained an
  134. unterminated quote, the following line inherits the quoted state.
  135. </p>
  136. <p>Traditional preprocessors replace parameters in the replacement text
  137. with their arguments regardless of whether the parameters are within
  138. quotes or not. This provides a way to stringize arguments. For
  139. example
  140. </p>
  141. <div class="smallexample">
  142. <pre class="smallexample">#define str(x) &quot;x&quot;
  143. str(/* <span class="roman">A comment</span> */some text )
  144. &rarr; &quot;some text &quot;
  145. </pre></div>
  146. <p>Note that the comment is removed, but that the trailing space is
  147. preserved. Here is an example of using a comment to effect token
  148. pasting.
  149. </p>
  150. <div class="smallexample">
  151. <pre class="smallexample">#define suffix(x) foo_/**/x
  152. suffix(bar)
  153. &rarr; foo_bar
  154. </pre></div>
  155. <hr>
  156. <div class="header">
  157. <p>
  158. Next: <a href="Traditional-miscellany.html#Traditional-miscellany" accesskey="n" rel="next">Traditional miscellany</a>, Previous: <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis" accesskey="p" rel="prev">Traditional lexical analysis</a>, Up: <a href="Traditional-Mode.html#Traditional-Mode" accesskey="u" rel="up">Traditional Mode</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>
  159. </div>
  160. </body>
  161. </html>