You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Macro-Arguments.html 8.7KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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>Macro Arguments (The C Preprocessor)</title>
  21. <meta name="description" content="Macro Arguments (The C Preprocessor)">
  22. <meta name="keywords" content="Macro Arguments (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="Macros.html#Macros" rel="up" title="Macros">
  30. <link href="Stringizing.html#Stringizing" rel="next" title="Stringizing">
  31. <link href="Function_002dlike-Macros.html#Function_002dlike-Macros" rel="prev" title="Function-like 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="Macro-Arguments"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Stringizing.html#Stringizing" accesskey="n" rel="next">Stringizing</a>, Previous: <a href="Function_002dlike-Macros.html#Function_002dlike-Macros" accesskey="p" rel="prev">Function-like Macros</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</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="Macro-Arguments-1"></a>
  68. <h3 class="section">3.3 Macro Arguments</h3>
  69. <a name="index-arguments"></a>
  70. <a name="index-macros-with-arguments"></a>
  71. <a name="index-arguments-in-macro-definitions"></a>
  72. <p>Function-like macros can take <em>arguments</em>, just like true functions.
  73. To define a macro that uses arguments, you insert <em>parameters</em>
  74. between the pair of parentheses in the macro definition that make the
  75. macro function-like. The parameters must be valid C identifiers,
  76. separated by commas and optionally whitespace.
  77. </p>
  78. <p>To invoke a macro that takes arguments, you write the name of the macro
  79. followed by a list of <em>actual arguments</em> in parentheses, separated
  80. by commas. The invocation of the macro need not be restricted to a
  81. single logical line&mdash;it can cross as many lines in the source file as
  82. you wish. The number of arguments you give must match the number of
  83. parameters in the macro definition. When the macro is expanded, each
  84. use of a parameter in its body is replaced by the tokens of the
  85. corresponding argument. (You need not use all of the parameters in the
  86. macro body.)
  87. </p>
  88. <p>As an example, here is a macro that computes the minimum of two numeric
  89. values, as it is defined in many C programs, and some uses.
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample">#define min(X, Y) ((X) &lt; (Y) ? (X) : (Y))
  93. x = min(a, b); &rarr; x = ((a) &lt; (b) ? (a) : (b));
  94. y = min(1, 2); &rarr; y = ((1) &lt; (2) ? (1) : (2));
  95. z = min(a + 28, *p); &rarr; z = ((a + 28) &lt; (*p) ? (a + 28) : (*p));
  96. </pre></div>
  97. <p>(In this small example you can already see several of the dangers of
  98. macro arguments. See <a href="Macro-Pitfalls.html#Macro-Pitfalls">Macro Pitfalls</a>, for detailed explanations.)
  99. </p>
  100. <p>Leading and trailing whitespace in each argument is dropped, and all
  101. whitespace between the tokens of an argument is reduced to a single
  102. space. Parentheses within each argument must balance; a comma within
  103. such parentheses does not end the argument. However, there is no
  104. requirement for square brackets or braces to balance, and they do not
  105. prevent a comma from separating arguments. Thus,
  106. </p>
  107. <div class="smallexample">
  108. <pre class="smallexample">macro (array[x = y, x + 1])
  109. </pre></div>
  110. <p>passes two arguments to <code>macro</code>: <code>array[x = y</code> and <code>x +
  111. 1]</code>. If you want to supply <code>array[x = y, x + 1]</code> as an argument,
  112. you can write it as <code>array[(x = y, x + 1)]</code>, which is equivalent C
  113. code.
  114. </p>
  115. <p>All arguments to a macro are completely macro-expanded before they are
  116. substituted into the macro body. After substitution, the complete text
  117. is scanned again for macros to expand, including the arguments. This rule
  118. may seem strange, but it is carefully designed so you need not worry
  119. about whether any function call is actually a macro invocation. You can
  120. run into trouble if you try to be too clever, though. See <a href="Argument-Prescan.html#Argument-Prescan">Argument Prescan</a>, for detailed discussion.
  121. </p>
  122. <p>For example, <code>min (min (a, b), c)</code> is first expanded to
  123. </p>
  124. <div class="smallexample">
  125. <pre class="smallexample"> min (((a) &lt; (b) ? (a) : (b)), (c))
  126. </pre></div>
  127. <p>and then to
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">((((a) &lt; (b) ? (a) : (b))) &lt; (c)
  131. ? (((a) &lt; (b) ? (a) : (b)))
  132. : (c))
  133. </pre></div>
  134. <p>(Line breaks shown here for clarity would not actually be generated.)
  135. </p>
  136. <a name="index-empty-macro-arguments"></a>
  137. <p>You can leave macro arguments empty; this is not an error to the
  138. preprocessor (but many macros will then expand to invalid code).
  139. You cannot leave out arguments entirely; if a macro takes two arguments,
  140. there must be exactly one comma at the top level of its argument list.
  141. Here are some silly examples using <code>min</code>:
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample">min(, b) &rarr; (( ) &lt; (b) ? ( ) : (b))
  145. min(a, ) &rarr; ((a ) &lt; ( ) ? (a ) : ( ))
  146. min(,) &rarr; (( ) &lt; ( ) ? ( ) : ( ))
  147. min((,),) &rarr; (((,)) &lt; ( ) ? ((,)) : ( ))
  148. min() error&rarr; macro &quot;min&quot; requires 2 arguments, but only 1 given
  149. min(,,) error&rarr; macro &quot;min&quot; passed 3 arguments, but takes just 2
  150. </pre></div>
  151. <p>Whitespace is not a preprocessing token, so if a macro <code>foo</code> takes
  152. one argument, <code>foo&nbsp;()<!-- /@w --></code> and <code>foo&nbsp;(&nbsp;)<!-- /@w --></code> both supply it an
  153. empty argument. Previous GNU preprocessor implementations and
  154. documentation were incorrect on this point, insisting that a
  155. function-like macro that takes a single argument be passed a space if an
  156. empty argument was required.
  157. </p>
  158. <p>Macro parameters appearing inside string literals are not replaced by
  159. their corresponding actual arguments.
  160. </p>
  161. <div class="smallexample">
  162. <pre class="smallexample">#define foo(x) x, &quot;x&quot;
  163. foo(bar) &rarr; bar, &quot;x&quot;
  164. </pre></div>
  165. <hr>
  166. <div class="header">
  167. <p>
  168. Next: <a href="Stringizing.html#Stringizing" accesskey="n" rel="next">Stringizing</a>, Previous: <a href="Function_002dlike-Macros.html#Function_002dlike-Macros" accesskey="p" rel="prev">Function-like Macros</a>, Up: <a href="Macros.html#Macros" accesskey="u" rel="up">Macros</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>
  169. </div>
  170. </body>
  171. </html>