diff options
Diffstat (limited to 'devdocs/c/variadic%2Fva_arg.html')
| -rw-r--r-- | devdocs/c/variadic%2Fva_arg.html | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/devdocs/c/variadic%2Fva_arg.html b/devdocs/c/variadic%2Fva_arg.html new file mode 100644 index 00000000..841667fa --- /dev/null +++ b/devdocs/c/variadic%2Fva_arg.html @@ -0,0 +1,54 @@ + <h1 id="firstHeading" class="firstHeading">va_arg</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdarg.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">T va_arg( va_list ap, T );</pre> +</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> </table> <p>The <code>va_arg</code> macro expands to an expression of type <code>T</code> that corresponds to the next parameter from the <code>va_list</code> <code>ap</code>.</p> +<p>Prior to calling <code>va_arg</code>, <code>ap</code> must be initialized by a call to either <code>va_start</code> or <code>va_copy</code>, with no intervening call to <code>va_end</code>. Each invocation of the <code>va_arg</code> macro modifies <code>ap</code> to point to the next variable argument.</p> +<p>If the type of the next argument in <code>ap</code> (after promotions) is not compatible with <code>T</code>, the behavior is undefined, unless:</p> +<ul> +<li> one type is a signed integer type, the other type is the corresponding unsigned integer type, and the value is representable in both types; or </li> +<li> one type is pointer to <code>void</code> and the other is a pointer to a character type. </li> +</ul> <p>If <code>va_arg</code> is called when there are no more arguments in <code>ap</code>, the behavior is undefined.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> ap </td> <td> - </td> <td> an instance of the <code>va_list</code> type </td> +</tr> <tr class="t-par"> <td> T </td> <td> - </td> <td> the type of the next parameter in <code>ap</code> </td> +</tr> +</table> <h3 id="Expanded_value"> Expanded value</h3> <p>the next variable parameter in <code>ap</code></p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdarg.h> +#include <math.h> + +double stddev(int count, ...) +{ + double sum = 0; + double sum_sq = 0; + va_list args; + va_start(args, count); + for (int i = 0; i < count; ++i) { + double num = va_arg(args, double); + sum += num; + sum_sq += num*num; + } + va_end(args); + return sqrt(sum_sq/count - (sum/count)*(sum/count)); +} + +int main(void) +{ + printf("%f\n", stddev(4, 25.0, 27.3, 26.9, 25.7)); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">0.920258</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.16.1.1 The va_arg macro (p: 269-270) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.15.1.1 The va_arg macro (p: 249-250) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.8.1.2 The va_arg macro </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="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="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="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> <tr class="t-dsc"> <td> <div><a href="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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/variadic/va_arg" title="cpp/utility/variadic/va arg">C++ documentation</a></span> for <code>va_arg</code> </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/variadic/va_arg" class="_attribution-link">https://en.cppreference.com/w/c/variadic/va_arg</a> + </p> +</div> |
