Traditional-macros.html 7.4 KB

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