summaryrefslogtreecommitdiff
path: root/devdocs/c/variadic%2Fva_start.html
blob: 8dd8a3653bee7281cd4f3c46b5b7179baa5bc622 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    <h1 id="firstHeading" class="firstHeading">va_start</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdarg.h&gt;</code> </th> <th> </th> <th> </th> </tr>   <tr class="t-dcl t-until-c23"> <td><pre data-language="c">void va_start( va_list ap, parmN );</pre></td> <td class="t-dcl-nopad"> </td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">void va_start( va_list ap, ... );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr>   </table> <p>The <code>va_start</code> macro enables access to the variable arguments<span class="t-rev-inl t-until-c23"><span> following the named argument <code>parmN</code></span><span><span class="t-mark-rev t-until-c23">(until C23)</span></span></span>.</p>
<p><code>va_start</code> shall be invoked with an instance to a valid <code>va_list</code> object <code>ap</code> before any calls to <code>va_arg</code>.</p>
<table class="t-rev-begin"> <tr class="t-rev t-until-c23">
<td> <p>If <code>parmN</code> is declared with <code>register</code> storage class specifier, with an array type, with a function type, or with a type not compatible with the type that results from default argument promotions, the behavior is undefined.</p>
</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td>
</tr> <tr class="t-rev t-since-c23">
<td> <p>Only the first argument passed to <code>va_start</code> is evaluated. Any additional arguments are neither expanded nor used in any way.</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table>  <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> parmN </td> <td> - </td> <td> the named parameter preceding the first variable parameter </td>
</tr>
</table> <h3 id="Expanded_value"> Expanded value</h3> <p>(none)</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;stdarg.h&gt;
 
int add_nums_C99(int count, ...)
{
    int result = 0;
    va_list args;
    va_start(args, count); // count can be omitted since C23
 
    for (int i = 0; i &lt; count; ++i) {
        result += va_arg(args, int);
    }
 
    va_end(args);
    return result;
}
 
#if __STDC_VERSION__ &gt; 201710L
// Same as above, valid since C23
int add_nums_C23(...)
{
    int result = 0;
    va_list args;
    va_start(args);
 
    int count = va_arg(args, int);
    for (int i = 0; i &lt; count; ++i) {
        result += va_arg(args, int);
    }
 
    va_end(args);
    return result;
}
#endif
 
int main(void)
{
    printf("%d\n", add_nums_C99(4, 25, 25, 50, 50));
#if __STDC_VERSION__ &gt; 201710L
    printf("%d\n", add_nums_C23(4, 25, 25, 50, 50));
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">150
150</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.16.1.4 The va_start macro (p: 198-199) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.16.1.4 The va_start macro (p: 271-272) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.15.1.4 The va_start macro (p: 251-252) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.8.1.1 The va_start 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_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="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 colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/variadic/va_start" title="cpp/utility/variadic/va start">C++ documentation</a></span> for <code>va_start</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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_start" class="_attribution-link">https://en.cppreference.com/w/c/variadic/va_start</a>
  </p>
</div>