Traditional-macros.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>The C Preprocessor: Traditional macros</title>
  20. <meta name="description" content="The C Preprocessor: Traditional macros">
  21. <meta name="keywords" content="The C Preprocessor: Traditional macros">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Traditional-macros"></a>
  63. <div class="header">
  64. <p>
  65. 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>
  66. </div>
  67. <hr>
  68. <a name="Traditional-macros-1"></a>
  69. <h3 class="section">10.2 Traditional macros</h3>
  70. <p>The major difference between traditional and ISO macros is that the
  71. former expand to text rather than to a token sequence. CPP removes
  72. all leading and trailing horizontal whitespace from a macro&rsquo;s
  73. replacement text before storing it, but preserves the form of internal
  74. whitespace.
  75. </p>
  76. <p>One consequence is that it is legitimate for the replacement text to
  77. contain an unmatched quote (see <a href="Traditional-lexical-analysis.html#Traditional-lexical-analysis">Traditional lexical analysis</a>). An
  78. unclosed string or character constant continues into the text
  79. following the macro call. Similarly, the text at the end of a macro&rsquo;s
  80. expansion can run together with the text after the macro invocation to
  81. produce a single token.
  82. </p>
  83. <p>Normally comments are removed from the replacement text after the
  84. macro is expanded, but if the <samp>-CC</samp> option is passed on the
  85. command-line comments are preserved. (In fact, the current
  86. implementation removes comments even before saving the macro
  87. replacement text, but it careful to do it in such a way that the
  88. observed effect is identical even in the function-like macro case.)
  89. </p>
  90. <p>The ISO stringizing operator &lsquo;<samp>#</samp>&rsquo; and token paste operator
  91. &lsquo;<samp>##</samp>&rsquo; have no special meaning. As explained later, an effect
  92. similar to these operators can be obtained in a different way. Macro
  93. names that are embedded in quotes, either from the main file or after
  94. macro replacement, do not expand.
  95. </p>
  96. <p>CPP replaces an unquoted object-like macro name with its replacement
  97. text, and then rescans it for further macros to replace. Unlike
  98. standard macro expansion, traditional macro expansion has no provision
  99. to prevent recursion. If an object-like macro appears unquoted in its
  100. replacement text, it will be replaced again during the rescan pass,
  101. and so on <em>ad infinitum</em>. GCC detects when it is expanding
  102. recursive macros, emits an error message, and continues after the
  103. offending macro invocation.
  104. </p>
  105. <div class="smallexample">
  106. <pre class="smallexample">#define PLUS +
  107. #define INC(x) PLUS+x
  108. INC(foo);
  109. &rarr; ++foo;
  110. </pre></div>
  111. <p>Function-like macros are similar in form but quite different in
  112. behavior to their ISO counterparts. Their arguments are contained
  113. within parentheses, are comma-separated, and can cross physical lines.
  114. Commas within nested parentheses are not treated as argument
  115. separators. Similarly, a quote in an argument cannot be left
  116. unclosed; a following comma or parenthesis that comes before the
  117. closing quote is treated like any other character. There is no
  118. facility for handling variadic macros.
  119. </p>
  120. <p>This implementation removes all comments from macro arguments, unless
  121. the <samp>-C</samp> option is given. The form of all other horizontal
  122. whitespace in arguments is preserved, including leading and trailing
  123. whitespace. In particular
  124. </p>
  125. <div class="smallexample">
  126. <pre class="smallexample">f( )
  127. </pre></div>
  128. <p>is treated as an invocation of the macro &lsquo;<samp>f</samp>&rsquo; with a single
  129. argument consisting of a single space. If you want to invoke a
  130. function-like macro that takes no arguments, you must not leave any
  131. whitespace between the parentheses.
  132. </p>
  133. <p>If a macro argument crosses a new line, the new line is replaced with
  134. a space when forming the argument. If the previous line contained an
  135. unterminated quote, the following line inherits the quoted state.
  136. </p>
  137. <p>Traditional preprocessors replace parameters in the replacement text
  138. with their arguments regardless of whether the parameters are within
  139. quotes or not. This provides a way to stringize arguments. For
  140. example
  141. </p>
  142. <div class="smallexample">
  143. <pre class="smallexample">#define str(x) &quot;x&quot;
  144. str(/* <span class="roman">A comment</span> */some text )
  145. &rarr; &quot;some text &quot;
  146. </pre></div>
  147. <p>Note that the comment is removed, but that the trailing space is
  148. preserved. Here is an example of using a comment to effect token
  149. pasting.
  150. </p>
  151. <div class="smallexample">
  152. <pre class="smallexample">#define suffix(x) foo_/**/x
  153. suffix(bar)
  154. &rarr; foo_bar
  155. </pre></div>
  156. <hr>
  157. <div class="header">
  158. <p>
  159. 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>
  160. </div>
  161. </body>
  162. </html>