diff options
Diffstat (limited to 'devdocs/gcc~13/variadic-macros.html')
| -rw-r--r-- | devdocs/gcc~13/variadic-macros.html | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/devdocs/gcc~13/variadic-macros.html b/devdocs/gcc~13/variadic-macros.html new file mode 100644 index 00000000..1660e7f9 --- /dev/null +++ b/devdocs/gcc~13/variadic-macros.html @@ -0,0 +1,10 @@ +<div class="section-level-extent" id="Variadic-Macros"> <div class="nav-panel"> <p> Next: <a href="escaped-newlines" accesskey="n" rel="next">Slightly Looser Rules for Escaped Newlines</a>, Previous: <a href="variable-length" accesskey="p" rel="prev">Arrays of Variable Length</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Macros-with-a-Variable-Number-of-Arguments_002e"><span>6.21 Macros with a Variable Number of Arguments.<a class="copiable-link" href="#Macros-with-a-Variable-Number-of-Arguments_002e"> ¶</a></span></h1> <p>In the ISO C standard of 1999, a macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)</pre> +</div> <p>Here ‘<samp class="samp">…</samp>’ is a <em class="dfn">variable argument</em>. In the invocation of such a macro, it represents the zero or more tokens until the closing parenthesis that ends the invocation, including any commas. This set of tokens replaces the identifier <code class="code">__VA_ARGS__</code> in the macro body wherever it appears. See the CPP manual for more information. </p> <p>GCC has long supported variadic macros, and used a different syntax that allowed you to give a name to the variable arguments just like any other argument. Here is an example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define debug(format, args...) fprintf (stderr, format, args)</pre> +</div> <p>This is in all ways equivalent to the ISO C example above, but arguably more readable and descriptive. </p> <p>GNU CPP has two further variadic macro extensions, and permits them to be used with either of the above forms of macro definition. </p> <p>In standard C, you are not allowed to leave the variable argument out entirely; but you are allowed to pass an empty argument. For example, this invocation is invalid in ISO C, because there is no comma after the string: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">debug ("A message")</pre> +</div> <p>GNU CPP permits you to completely omit the variable arguments in this way. In the above examples, the compiler would complain, though since the expansion of the macro still has the extra comma after the format string. </p> <p>To help solve this problem, CPP behaves specially for variable arguments used with the token paste operator, ‘<samp class="samp">##</samp>’. If instead you write </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)</pre> +</div> <p>and if the variable arguments are omitted or empty, the ‘<samp class="samp">##</samp>’ operator causes the preprocessor to remove the comma before it. If you do provide some variable arguments in your macro invocation, GNU CPP does not complain about the paste operation and instead places the variable arguments after the comma. Just like any other pasted macro argument, these arguments are not macro expanded. </p> </div> <div class="nav-panel"> <p> Next: <a href="escaped-newlines">Slightly Looser Rules for Escaped Newlines</a>, Previous: <a href="variable-length">Arrays of Variable Length</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution"> + <p class="_attribution-p"> + © Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br> + <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Variadic-Macros.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Variadic-Macros.html</a> + </p> +</div> |
