diff options
Diffstat (limited to 'devdocs/c/language%2Fvariadic.html')
| -rw-r--r-- | devdocs/c/language%2Fvariadic.html | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/devdocs/c/language%2Fvariadic.html b/devdocs/c/language%2Fvariadic.html new file mode 100644 index 00000000..610036e5 --- /dev/null +++ b/devdocs/c/language%2Fvariadic.html @@ -0,0 +1,73 @@ + <h1 id="firstHeading" class="firstHeading">Variadic arguments</h1> <p>Variadic functions are functions that may be called with different number of arguments.</p> +<p>Only prototyped <a href="function_declaration" title="c/language/function declaration">function declarations</a> may be variadic. This is indicated by the parameter of the form <code>...</code> which must appear last in the parameter list<span class="t-rev-inl t-until-c23"><span> and must follow at least one named parameter</span><span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span>. The ellipsis parameter and the proceeding parameter must be delimited by <code>,</code>.</p> +<div class="c source-c"><pre data-language="c">// Prototyped declaration +int printx(const char* fmt, ...); // function declared this way +printx("hello world"); // may be called with one +printx("a=%d b=%d", a, b); // or more arguments + +int printz(...); // OK since C23 and in C++ +// Error until C23: ... must follow at least one named parameter + +// int printy(..., const char* fmt); // Error: ... must be the last +// int printa(const char* fmt...); // Error in C: ',' is required; OK in C++</pre></div> <p>At the <a href="operator_other#Function_call" title="c/language/operator other">function call</a>, each argument that is a part of the variable argument list undergoes special implicit conversions known as <a href="conversion#Default_argument_promotions" title="c/language/conversion">default argument promotions</a>.</p> +<p>Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the <a href="../variadic" title="c/variadic"><code><stdarg.h></code> library facilities</a>:</p> +<table class="t-dsc-begin"> <tr class="t-dsc-header"> <th colspan="2"> Defined in header <code><stdarg.h></code> </th> +</tr> <tr class="t-dsc"> <td> <div><a href="../variadic/va_start" title="c/variadic/va start"> <span class="t-lines"><span>va_start</span></span></a></div> </td> <td> enables access to variadic function arguments <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../variadic/va_arg" title="c/variadic/va arg"> <span class="t-lines"><span>va_arg</span></span></a></div> </td> <td> accesses the next variadic function argument <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../variadic/va_copy" title="c/variadic/va copy"> <span class="t-lines"><span>va_copy</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div> </td> <td> makes a copy of the variadic function arguments <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../variadic/va_end" title="c/variadic/va end"> <span class="t-lines"><span>va_end</span></span></a></div> </td> <td> ends traversal of the variadic function arguments <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="../variadic/va_list" title="c/variadic/va list"> <span class="t-lines"><span>va_list</span></span></a></div> </td> <td> holds the information needed by va_start, va_arg, va_end, and va_copy <br> <span class="t-mark">(typedef)</span> </td> +</tr> </table> <h3 id="Notes"> Notes</h3> <p>Although old-style (prototype-less) <a href="function_declaration" title="c/language/function declaration">function declarations</a> allow the subsequent function calls to use any number of arguments, they are not allowed to be variadic (as of C89). The definition of such function must specify a fixed number of parameters and cannot use the <code>stdarg.h</code> macros.</p> +<div class="c source-c"><pre data-language="c">// old-style declaration, removed in C23 +int printx(); // function declared this way +printx("hello world"); // may be called with one +printx("a=%d b=%d", a, b); // or more arguments +// the behavior of at least one of these calls is undefined, depending on +// the number of parameters the function is defined to take</pre></div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <time.h> +#include <stdarg.h> + +void tlog(const char* fmt,...) +{ + char msg[50]; + strftime(msg, sizeof msg, "%T", localtime(&(time_t){time(NULL)})); + printf("[%s] ", msg); + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +} + +int main(void) +{ + tlog("logging %d %d %d...\n", 1, 2, 3); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">[10:21:38] logging 1 2 3...</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 6.7.6.3/9 Function declarators (including prototypes) (p: 96) </li> +<li> 7.16 Variable arguments <stdarg.h> (p: 197-199) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 6.7.6.3/9 Function declarators (including prototypes) (p: 133) </li> +<li> 7.16 Variable arguments <stdarg.h> (p: 269-272) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul> +<li> 6.7.5.3/9 Function declarators (including prototypes) (p: 119) </li> +<li> 7.15 Variable arguments <stdarg.h> (p: 249-252) </li> +</ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul> +<li> 3.5.4.3/5 Function declarators (including prototypes) </li> +<li> 4.8 VARIABLE ARGUMENTS <stdarg.h> </li> +</ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/variadic_arguments" title="cpp/language/variadic arguments">C++ documentation</a></span> for <span class=""><span>Variadic arguments</span></span> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/language/variadic" class="_attribution-link">https://en.cppreference.com/w/c/language/variadic</a> + </p> +</div> |
