Variadic-Macros.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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: Variadic Macros</title>
  20. <meta name="description" content="The C Preprocessor: Variadic Macros">
  21. <meta name="keywords" content="The C Preprocessor: Variadic 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="Macros.html#Macros" rel="up" title="Macros">
  30. <link href="Predefined-Macros.html#Predefined-Macros" rel="next" title="Predefined Macros">
  31. <link href="Concatenation.html#Concatenation" rel="prev" title="Concatenation">
  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="Variadic-Macros"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Predefined-Macros.html#Predefined-Macros" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html#Concatenation" accesskey="p" rel="prev">Concatenation</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="Variadic-Macros-1"></a>
  69. <h3 class="section">3.6 Variadic Macros</h3>
  70. <a name="index-variable-number-of-arguments"></a>
  71. <a name="index-macros-with-variable-arguments"></a>
  72. <a name="index-variadic-macros"></a>
  73. <p>A macro can be declared to accept a variable number of arguments much as
  74. a function can. The syntax for defining the macro is similar to that of
  75. a function. Here is an example:
  76. </p>
  77. <div class="smallexample">
  78. <pre class="smallexample">#define eprintf(&hellip;) fprintf (stderr, __VA_ARGS__)
  79. </pre></div>
  80. <p>This kind of macro is called <em>variadic</em>. When the macro is invoked,
  81. all the tokens in its argument list after the last named argument (this
  82. macro has none), including any commas, become the <em>variable
  83. argument</em>. This sequence of tokens replaces the identifier
  84. <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> in the macro body wherever it appears. Thus, we
  85. have this expansion:
  86. </p>
  87. <div class="smallexample">
  88. <pre class="smallexample">eprintf (&quot;%s:%d: &quot;, input_file, lineno)
  89. &rarr; fprintf (stderr, &quot;%s:%d: &quot;, input_file, lineno)
  90. </pre></div>
  91. <p>The variable argument is completely macro-expanded before it is inserted
  92. into the macro expansion, just like an ordinary argument. You may use
  93. the &lsquo;<samp>#</samp>&rsquo; and &lsquo;<samp>##</samp>&rsquo; operators to stringize the variable argument
  94. or to paste its leading or trailing token with another token. (But see
  95. below for an important special case for &lsquo;<samp>##</samp>&rsquo;.)
  96. </p>
  97. <p>If your macro is complicated, you may want a more descriptive name for
  98. the variable argument than <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>. CPP permits
  99. this, as an extension. You may write an argument name immediately
  100. before the &lsquo;<samp>&hellip;</samp>&rsquo;; that name is used for the variable argument.
  101. The <code>eprintf</code> macro above could be written
  102. </p>
  103. <div class="smallexample">
  104. <pre class="smallexample">#define eprintf(args&hellip;) fprintf (stderr, args)
  105. </pre></div>
  106. <p>using this extension. You cannot use <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code> and this
  107. extension in the same macro.
  108. </p>
  109. <p>You can have named arguments as well as variable arguments in a variadic
  110. macro. We could define <code>eprintf</code> like this, instead:
  111. </p>
  112. <div class="smallexample">
  113. <pre class="smallexample">#define eprintf(format, &hellip;) fprintf (stderr, format, __VA_ARGS__)
  114. </pre></div>
  115. <p>This formulation looks more descriptive, but unfortunately it is less
  116. flexible: you must now supply at least one argument after the format
  117. string. In standard C, you cannot omit the comma separating the named
  118. argument from the variable arguments. Furthermore, if you leave the
  119. variable argument empty, you will get a syntax error, because
  120. there will be an extra comma after the format string.
  121. </p>
  122. <div class="smallexample">
  123. <pre class="smallexample">eprintf(&quot;success!\n&quot;, );
  124. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  125. </pre></div>
  126. <p>GNU CPP has a pair of extensions which deal with this problem. First,
  127. you are allowed to leave the variable argument out entirely:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample">eprintf (&quot;success!\n&quot;)
  131. &rarr; fprintf(stderr, &quot;success!\n&quot;, );
  132. </pre></div>
  133. <p>Second, the &lsquo;<samp>##</samp>&rsquo; token paste operator has a special meaning when
  134. placed between a comma and a variable argument. If you write
  135. </p>
  136. <div class="smallexample">
  137. <pre class="smallexample">#define eprintf(format, &hellip;) fprintf (stderr, format, ##__VA_ARGS__)
  138. </pre></div>
  139. <p>and the variable argument is left out when the <code>eprintf</code> macro is
  140. used, then the comma before the &lsquo;<samp>##</samp>&rsquo; will be deleted. This does
  141. <em>not</em> happen if you pass an empty argument, nor does it happen if
  142. the token preceding &lsquo;<samp>##</samp>&rsquo; is anything other than a comma.
  143. </p>
  144. <div class="smallexample">
  145. <pre class="smallexample">eprintf (&quot;success!\n&quot;)
  146. &rarr; fprintf(stderr, &quot;success!\n&quot;);
  147. </pre></div>
  148. <p>The above explanation is ambiguous about the case where the only macro
  149. parameter is a variable arguments parameter, as it is meaningless to
  150. try to distinguish whether no argument at all is an empty argument or
  151. a missing argument.
  152. CPP retains the comma when conforming to a specific C
  153. standard. Otherwise the comma is dropped as an extension to the standard.
  154. </p>
  155. <p>The C standard
  156. mandates that the only place the identifier <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>
  157. can appear is in the replacement list of a variadic macro. It may not
  158. be used as a macro name, macro argument name, or within a different type
  159. of macro. It may also be forbidden in open text; the standard is
  160. ambiguous. We recommend you avoid using it except for its defined
  161. purpose.
  162. </p>
  163. <p>Variadic macros became a standard part of the C language with C99.
  164. GNU CPP previously supported them
  165. with a named variable argument
  166. (&lsquo;<samp>args&hellip;</samp>&rsquo;, not &lsquo;<samp>&hellip;</samp>&rsquo; and <code><span class="nolinebreak">__VA_ARGS__</span><!-- /@w --></code>), which
  167. is still supported for backward compatibility.
  168. </p>
  169. <hr>
  170. <div class="header">
  171. <p>
  172. Next: <a href="Predefined-Macros.html#Predefined-Macros" accesskey="n" rel="next">Predefined Macros</a>, Previous: <a href="Concatenation.html#Concatenation" accesskey="p" rel="prev">Concatenation</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>
  173. </div>
  174. </body>
  175. </html>