Macro-Arguments.html 8.8 KB

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