<!DOCTYPE html>
<html class="client-js"><head>
  <meta charset="UTF-8">
  <title>stdarg.h</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script>
    function importScript() { return 1 } // this is to avoid the error from site.js
  </script>
  <script>let articleId = 'Stdarg.h'</script>
  <link rel="canonical" href="https://en.wikipedia.org/wiki/Stdarg.h">
  <link href="../-/mw/ext.cite.ux-enhancements.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="../-/mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="../-/mw/skins.minerva.base.reset|skins.minerva.content.styles|ext.cite.style|site.styles|mobile.app.pagestyles.android|mediawiki.page.gallery.styles|mediawiki.skinning.content.parsoid.css" rel="stylesheet" type="text/css">
  <link href="../-/style.css" rel="stylesheet" type="text/css"><link href="../-/content.parsoid.css" rel="stylesheet" type="text/css"><link href="../-/inserted_style.css" rel="stylesheet" type="text/css">
  <script src="../-/script.js"></script><script src="../-/masonry.min.js"></script><script src="../-/article_list_home.js"></script><script src="../-/images_loaded.min.js"></script><script src="../-/node_module/details-element-polyfill/dist/details-element-polyfill.js"></script>
</head>

<body class="mediawiki mw-hide-empty-elt ns-0 ns-subject stable skin-minerva action-view animations">
  <div id="mw-mf-viewport" class="feature-header-v2">
    <div id="mw-mf-page-center">
      <div id="content" class="mw-body">
        <a id="top"></a>
        <div id="bodyContent" class="content mw-parser-output">
          
          <div id="mw-content-text" style="direction: ltr;"><h1 class="section-heading" tabindex="0" aria-haspopup="true" data-section-id="0">
  <span class="mw-headline" id="title_0">stdarg.h</span>
</h1>
<div id="mf-section-0" class="mf-section-0" aria-pressed="true" aria-expanded="true">
  <p>
<style data-mw-deduplicate="TemplateStyles:r1002487873">.mw-parser-output .sidebar{width:22em;float:right;clear:right;margin:0.5em 0 1em 1em;background:#f8f9fa;border:1px solid #aaa;padding:0.2em;border-spacing:0.4em 0;text-align:center;line-height:1.4em;font-size:88%}.mw-parser-output .sidebar a{white-space:nowrap}.mw-parser-output .sidebar-wraplinks a{white-space:normal}.mw-parser-output .sidebar-subgroup{width:100%;margin:0;border-spacing:0}.mw-parser-output .sidebar-left{float:left;clear:left;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-none{float:none;clear:both;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-outer-title{padding-bottom:0.2em;font-size:125%;line-height:1.2em;font-weight:bold}.mw-parser-output .sidebar-top-image{padding:0.4em 0}.mw-parser-output .sidebar-top-caption,.mw-parser-output .sidebar-pretitle-with-top-image,.mw-parser-output .sidebar-caption{padding-top:0.2em;line-height:1.2em}.mw-parser-output .sidebar-pretitle{padding-top:0.4em;line-height:1.2em}.mw-parser-output .sidebar-title,.mw-parser-output .sidebar-title-with-pretitle{padding:0.2em 0.4em;font-size:145%;line-height:1.2em}.mw-parser-output .sidebar-title-with-pretitle{padding-top:0}.mw-parser-output .sidebar-image{padding:0.2em 0 0.4em}.mw-parser-output .sidebar-heading{padding:0.1em}.mw-parser-output .sidebar-content{padding:0 0.1em 0.4em}.mw-parser-output .sidebar-content-with-subgroup{padding:0.1em 0 0.2em}.mw-parser-output .sidebar-above,.mw-parser-output .sidebar-below{padding:0.3em 0.4em;font-weight:bold}.mw-parser-output .sidebar-collapse .sidebar-above,.mw-parser-output .sidebar-collapse .sidebar-below{border-top:1px solid #aaa;border-bottom:1px solid #aaa}.mw-parser-output .sidebar-navbar{text-align:right;font-size:115%}.mw-parser-output .sidebar-collapse .sidebar-navbar{padding-top:0.6em}.mw-parser-output .sidebar-collapse .mw-collapsible-toggle{margin-top:0.2em}.mw-parser-output .sidebar-list-title{text-align:left;font-weight:bold;line-height:1.6em;font-size:105%}@media(max-width:720px){body.mediawiki .mw-parser-output .sidebar{width:100%!important;clear:both;float:none;margin-left:0!important;margin-right:0!important}}</style></p><table class="sidebar vertical-navbox nomobile"><tbody><tr><th class="sidebar-title"><a href="C_standard_library" title="C standard library">C standard library</a></th></tr><tr><th class="sidebar-heading">
General topics</th></tr><tr><td class="sidebar-content" style="text-align: left">
<ul><li><a href="C_data_types" title="C data types">Data types</a></li>
<li><a href="C_character_classification" title="C character classification">Character classification</a></li>
<li><a href="C_string_handling" title="C string handling">Strings</a></li>
<li><a href="C_mathematical_functions" title="C mathematical functions">Mathematics</a></li>
<li><a href="C_file_input/output" title="C file input/output">File input/output</a></li>
<li><a href="C_date_and_time_functions" title="C date and time functions">Date/time</a></li>
<li><a href="C_localization_functions" title="C localization functions">Localization</a></li>
<li><a href="C_dynamic_memory_allocation" title="C dynamic memory allocation">Memory allocation</a></li>
<li><a href="C_process_control" title="C process control">Process control</a></li>
<li><a href="C_signal_handling" title="C signal handling">Signals</a></li>
<li><a href="C_alternative_tokens" title="C alternative tokens">Alternative tokens</a></li></ul></td>
</tr><tr><th class="sidebar-heading">
Miscellaneous headers</th></tr><tr><td class="sidebar-content" style="text-align: left">
<ul><li><style data-mw-deduplicate="TemplateStyles:r886049734">.mw-parser-output .monospaced{font-family:monospace,monospace}</style><span class="monospaced">&lt;<a href="Assert.h" title="Assert.h">assert.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Errno.h" title="Errno.h">errno.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Setjmp.h" title="Setjmp.h">setjmp.h</a>&gt;</span></li>
<li><span class="monospaced">&lt;<a href="Stdarg.h" title="Stdarg.h">stdarg.h</a>&gt;</span></li></ul></td>
</tr><tr><td class="sidebar-navbar"><style data-mw-deduplicate="TemplateStyles:r992953826">.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}.mw-parser-output .infobox .navbar{font-size:100%}.mw-parser-output .navbox .navbar{display:block;font-size:100%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}</style></td></tr></tbody></table>
<p><span>
</span><meta property="mw:PageProp/displaytitle" content="stdarg.h"></p>

<p><b><code>stdarg.h</code></b> is a header in the <a href="C_standard_library" title="C standard library">C standard library</a> of the <a href="C_(programming_language)" title="C (programming language)">C programming language</a> that allows functions to accept <a href="Variadic_function#Example_in_C" title="Variadic function">an indefinite number of arguments</a>.<span class="mw-ref reference" id="cite_ref-1"><a href="#cite_note-1" style="counter-reset: mw-Ref 1;"><span class="mw-reflink-text">[1]</span></a></span> It provides facilities for stepping through a list of function arguments of unknown number and type.  <a href="C%2B%2B" title="C++">C++</a> provides this functionality in the header <code>cstdarg</code>.</p>

<p>The contents of <code>stdarg.h</code> are typically used in <a href="Variadic_function" title="Variadic function">variadic functions</a>, though they may be used in other functions (for example, <code><a href="Vprintf" title="Vprintf" class="mw-redirect">vprintf</a></code>) called by variadic functions.</p>


</div><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Declaring_variadic_functions">Declaring variadic functions</h2></summary>
    

<p><a href="Variadic_functions" title="Variadic functions" class="mw-redirect">Variadic functions</a> are functions which may take a variable number of arguments and are declared with an <a href="Ellipsis" title="Ellipsis">ellipsis</a> in place of the last parameter. An example of such a function is <code><a href="Printf" title="Printf" class="mw-redirect">printf</a></code>. A typical declaration is</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span> <span class="nf">check</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">double</span> <span class="n">b</span><span class="p">,</span> <span class="p">...);</span>
</pre></div>

<p>Variadic functions must have at least one named parameter, so, for instance,</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">char</span> <span class="o">*</span><span class="nf">wrong</span><span class="p">(...);</span>
</pre></div>

<p>is not allowed in C. (In C++, such a declaration is permitted.) In C, a comma must precede the ellipsis; in C++, it is optional.</p>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Defining_variadic_functions">Defining variadic functions</h2></summary>
    
<p>The same syntax is used in a definition:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">long</span> <span class="nf">func</span><span class="p">(</span><span class="kt">char</span><span class="p">,</span> <span class="kt">double</span><span class="p">,</span> <span class="kt">int</span><span class="p">,</span> <span class="p">...);</span>

<span class="kt">long</span> <span class="nf">func</span><span class="p">(</span><span class="kt">char</span> <span class="n">a</span><span class="p">,</span> <span class="kt">double</span> <span class="n">b</span><span class="p">,</span> <span class="kt">int</span> <span class="n">c</span><span class="p">,</span> <span class="p">...)</span>
<span class="p">{</span>
    <span class="cm">/* ... */</span>
<span class="p">}</span>
</pre></div>

<p>An ellipsis may not appear in old-style function definitions.</p>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="stdarg.h_types">stdarg.h types</h2></summary>
    

<table class="wikitable">
<tbody><tr>
<th>Name</th><th>Description</th><th>Compatibility</th></tr>
<tr>
<td><code><a href="http://en.cppreference.com/w/cpp/utility/variadic/va_list" class="external text external">va_list</a></code></td><td>type for iterating arguments</td><td><a href="C89_(C_version)" title="C89 (C version)" class="mw-redirect">C89</a></td></tr>
</tbody></table>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="stdarg.h_macros">stdarg.h macros</h2></summary>
    

<table class="wikitable">
<tbody><tr>
<th>Name</th><th>Description</th><th>compatibility</th></tr>
<tr>
<td><code><a href="http://en.cppreference.com/w/cpp/utility/variadic/va_start" class="external text external">va_start</a></code></td><td>Start iterating arguments with a <code>va_list</code></td><td>C89</td></tr>
<tr>
<td><code><a href="http://en.cppreference.com/w/cpp/utility/variadic/va_arg" class="external text external">va_arg</a></code></td><td>Retrieve an argument</td><td>C89</td></tr>
<tr>
<td><code><a href="http://en.cppreference.com/w/cpp/utility/variadic/va_end" class="external text external">va_end</a></code></td><td>Free a <code>va_list</code></td><td>C89</td></tr>
<tr>
<td><code><a href="http://en.cppreference.com/w/cpp/utility/variadic/va_copy" class="external text external">va_copy</a></code></td><td>Copy contents of one <code>va_list</code> to another</td><td><a href="C99" title="C99">C99</a></td></tr>
</tbody></table>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Accessing_the_arguments">Accessing the arguments</h2></summary>
    

<p>To access the unnamed arguments, one must declare a variable of type <code>va_list</code> in the variadic function. The macro <code>va_start</code> is then called with two arguments: the first is the variable declared of the type <code>va_list</code>, the second is the name of the last named parameter of the function. After this, each invocation of the <code>va_arg</code> macro yields the next argument. The first argument to <code>va_arg</code> is the <code>va_list</code> and the second is the type of the next argument passed to the function. Finally, the <code>va_end</code> macro must be called on the <code>va_list</code> before the function returns. (It is not required to read in all the arguments.)</p>

<p><a href="C99" title="C99">C99</a> provides an additional macro, <code>va_copy</code>, which can duplicate the state of a <code>va_list</code>. The macro invocation <code>va_copy(va2, va1)</code> copies <code>va1</code> into <code>va2</code>.</p>

<p>There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary.  Common conventions include:</p>
<ul><li>Use of a <code><a href="Printf" title="Printf" class="mw-redirect">printf</a></code> or <code><a href="Scanf" title="Scanf" class="mw-redirect">scanf</a></code>-like format string with embedded specifiers that indicate argument types.</li>
<li>A <a href="Sentinel_value" title="Sentinel value">sentinel value</a> at the end of the variadic arguments.</li>
<li>A count argument indicating the number of variadic arguments.</li></ul>


    <details data-level="3" open="">
    <summary class="section-heading"><h3 id="Passing_unnamed_arguments_to_other_calls">Passing unnamed arguments to other calls</h3></summary>
    
<p>Because the size of the unnamed argument list is generally unknown (the calling conventions employed by most compilers do not permit determining the size of the unnamed argument block pointed at by <code>va_list</code> inside the receiving function), there is also no reliable, generic way to forward the unnamed arguments into another variadic function. Even where determining the size of the argument list is possible by indirect means (for example, by parsing the format string of <code>fprintf()</code>), there is no portable way to pass the dynamically determined number of arguments into the inner variadic call, as the number and size of arguments passed into such calls must generally be known at compile time. To some extent, this restriction can be relaxed by employing <a href="Variadic_macro" title="Variadic macro">variadic macros</a> instead of variadic functions. Additionally, most standard library procedures provide <code>v</code>-prefixed alternative versions which accept a <i>reference</i> to the unnamed argument list (i.e. an initialized <code>va_list</code> variable) instead of the unnamed argument list itself. For example, <code>vfprintf()</code> is an alternate version of <code>fprintf()</code> expecting a <code>va_list</code> instead of the actual unnamed argument list. A user-defined variadic function can therefore initialize a <code>va_list</code> variable using <code>va_start</code> and pass it to an appropriate standard library function, in effect passing the unnamed argument list by reference instead of doing it by value. Because there is no reliable way to pass unnamed argument lists by value in C, providing variadic <a href="Application_programming_interface" title="Application programming interface" class="mw-redirect">API</a> functions without also providing equivalent functions accepting <code>va_list</code> instead is considered a bad programming practice.</p>


</details>

</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Type_safety">Type safety</h2></summary>
    

<p>Some C implementations provide C extensions that allow the compiler to check for the proper use of format strings and sentinels.  Barring these extensions, the compiler usually cannot check whether the unnamed arguments passed are of the type the function expects, or convert them to the required type. Therefore, care should be taken to ensure correctness in this regard, since <a href="Undefined_behavior" title="Undefined behavior">undefined behavior</a> results if the types do not match. For example, if the expected type is <code>int *</code>, then a null pointer should be passed as <code>(int *)NULL</code>. Writing just <code>NULL</code> would result in an argument of type either <code>int</code> or <code>void *</code>, neither of which is correct. Another consideration is the default argument <a href="Type_conversion#Type_promotion_in_C-like_languages" title="Type conversion">promotions</a> applied to the unnamed arguments. A <code>float</code> will automatically be promoted to a <code>double</code>. Likewise, arguments of types narrower than an <code>int</code> will be promoted to <code>int</code> or <code>unsigned int</code>. The function receiving the unnamed arguments must expect the promoted type.</p>

<p><a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a> has an extension that checks the passed arguments:</p>

<style data-mw-deduplicate="TemplateStyles:r996844942">.mw-parser-output .templatequote{overflow:hidden;margin:1em 0;padding:0 40px}.mw-parser-output .templatequote .templatequotecite{line-height:1.5em;text-align:left;padding-left:1.6em;margin-top:0}</style><blockquote class="templatequote">
<p><b><code>format(archetype, string-index, first-to-check)</code></b></p>

<p>The format attribute specifies that a function takes <code>printf</code>, <code>scanf</code>, <code>strftime</code> or <code>strfmon</code> style arguments which should be type-checked against a format string. For example, the declaration:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">extern</span> <span class="kt">int</span>
<span class="n">my_printf</span> <span class="p">(</span><span class="kt">void</span> <span class="o">*</span><span class="n">my_object</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">my_format</span><span class="p">,</span> <span class="p">...)</span>
      <span class="n">__attribute__</span> <span class="p">((</span><span class="n">format</span> <span class="p">(</span><span class="n">printf</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)));</span>
</pre></div><p>

causes the compiler to check the arguments in calls to <code>my_printf</code> for consistency with the <code>printf</code> style format string argument <code>my_format</code>.</p><div class="templatequotecite">—<span> </span><cite><style data-mw-deduplicate="TemplateStyles:r999302996">.mw-parser-output cite.citation{font-style:inherit}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .id-lock-free a,.mw-parser-output .citation .cs1-lock-free a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/6/65/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited a,.mw-parser-output .id-lock-registration a,.mw-parser-output .citation .cs1-lock-limited a,.mw-parser-output .citation .cs1-lock-registration a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/d/d6/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription a,.mw-parser-output .citation .cs1-lock-subscription a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/a/aa/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-ws-icon a{background:linear-gradient(transparent,transparent),url("//upload.wikimedia.org/wikipedia/commons/4/4c/Wikisource-logo.svg")right 0.1em center/12px no-repeat}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-maint{display:none;color:#33aa33;margin-left:0.3em}.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}</style><cite class="citation web cs1"><a href="https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Function-Attributes.html#Function-Attributes" class="external text external">"5.27 Extensions to the C Language Family - Declaring Attributes of Functions"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2009-01-03</span></span>.</cite></cite></div></blockquote>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="Example">Example</h2></summary>
    

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span>
<span class="cp">#include</span> <span class="cpf">&lt;stdarg.h&gt;</span>

<span class="cm">/* print all args one at a time until a negative argument is seen;</span>
<span class="cm">   all args are assumed to be of int type */</span>
<span class="kt">void</span> <span class="nf">printargs</span><span class="p">(</span><span class="kt">int</span> <span class="n">arg1</span><span class="p">,</span> <span class="p">...)</span>
<span class="p">{</span>
  <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
  <span class="kt">int</span> <span class="n">i</span><span class="p">;</span>

  <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">arg1</span><span class="p">);</span> 
  <span class="k">for</span> <span class="p">(</span><span class="n">i</span> <span class="o">=</span> <span class="n">arg1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">=</span> <span class="n">va_arg</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="kt">int</span><span class="p">))</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"%d "</span><span class="p">,</span> <span class="n">i</span><span class="p">);</span>
  <span class="n">va_end</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>
  <span class="n">putchar</span><span class="p">(</span><span class="sc">'\n'</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
   <span class="n">printargs</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">97</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">-1</span><span class="p">,</span> <span class="mi">48</span><span class="p">,</span> <span class="mi">-1</span><span class="p">);</span>
   <span class="n">printargs</span><span class="p">(</span><span class="mi">84</span><span class="p">,</span> <span class="mi">51</span><span class="p">,</span> <span class="mi">-1</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
   <span class="n">printargs</span><span class="p">(</span><span class="mi">-1</span><span class="p">);</span>
   <span class="n">printargs</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">-1</span><span class="p">);</span>
   <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>

<p>This program yields the output:</p>

<pre>5 2 14 84 97 15
84 51

1
</pre>

<p>To call other var args functions from within your function (such as sprintf) you need to use the var arg version of the function (vsprintf in this example):</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">void</span> <span class="nf">MyPrintf</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">format</span><span class="p">,</span> <span class="p">...)</span>
<span class="p">{</span>
  <span class="kt">va_list</span> <span class="n">args</span><span class="p">;</span>
  <span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="n">BUFSIZ</span><span class="p">];</span>

  <span class="n">va_start</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">format</span><span class="p">);</span>
  <span class="n">vsnprintf</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="k">sizeof</span> <span class="n">buffer</span><span class="p">,</span> <span class="n">format</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span>
  <span class="n">va_end</span><span class="p">(</span><span class="n">args</span><span class="p">);</span>
  <span class="n">FlushFunnyStream</span><span class="p">(</span><span class="n">buffer</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="varargs.h">varargs.h</h2></summary>
    
<p>Outdated versions of <a href="POSIX" title="POSIX">POSIX</a> defined the legacy header <code>varargs.h</code>, which dates from before the standardization of C and provides functionality similar to <code>stdarg.h</code>. This header is part of neither ISO C nor POSIX. The file, as defined in the second version of the <a href="Single_UNIX_Specification" title="Single UNIX Specification">Single UNIX Specification</a>, simply contains all of the functionality of C89 <code>stdarg.h</code>, with the exceptions that: it cannot be used in standard C new-style definitions; you may choose not to have a given argument (standard C requires at least one argument); and the way it works is different—in standard C, one would write:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdarg.h&gt;</span>

<span class="kt">int</span> <span class="nf">summate</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="p">...)</span>
<span class="p">{</span>
    <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(;</span> <span class="n">n</span><span class="p">;</span> <span class="n">n</span><span class="o">--</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="n">va_arg</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
    <span class="n">va_end</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">i</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>

<p>and call with</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="n">summate</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="n">summate</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
<span class="n">summate</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span>
</pre></div>

<p>With <code>varargs.h</code>, the function would be:</p>

<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="cp">#include</span> <span class="cpf">&lt;varargs.h&gt;</span>

<span class="n">summate</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">va_alist</span><span class="p">)</span>
    <span class="n">va_dcl</span> <span class="cm">/* no semicolon here! */</span>
<span class="p">{</span>
    <span class="kt">va_list</span> <span class="n">ap</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

    <span class="n">va_start</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>
    <span class="k">for</span> <span class="p">(;</span> <span class="n">n</span><span class="p">;</span> <span class="n">n</span><span class="o">--</span><span class="p">)</span>
        <span class="n">i</span> <span class="o">+=</span> <span class="n">va_arg</span><span class="p">(</span><span class="n">ap</span><span class="p">,</span> <span class="kt">int</span><span class="p">);</span>
    <span class="n">va_end</span><span class="p">(</span><span class="n">ap</span><span class="p">);</span>
    <span class="k">return</span> <span class="n">i</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>

<p>and is called the same way.</p>

<p><code>varargs.h</code> requires old-style function definitions because of the way the implementation works.<span class="mw-ref reference" id="cite_ref-2"><a href="#cite_note-2" style="counter-reset: mw-Ref 2;"><span class="mw-reflink-text">[2]</span></a></span> Conversely, it is not possible to mix old-style function definitions with <code>stdarg.h</code>.</p>


    
</details><details data-level="2" open="">
    <summary class="section-heading"><h2 id="References">References</h2></summary>
    
<div class="reflist " style=" list-style-type: decimal;">
<div class="mw-references-wrap"><ol class="mw-references references"><li id="cite_note-1"> <span id="mw-reference-text-cite_note-1" class="mw-reference-text"><cite class="citation web cs1"><a href="http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html" class="external text external">"IEEE Std 1003.1 <code>stdarg.h</code>"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2009-07-04</span></span>.</cite></span></li><li id="cite_note-2"> <span id="mw-reference-text-cite_note-2" class="mw-reference-text"><cite class="citation web cs1"><a href="http://www.opengroup.org/onlinepubs/007908799/xsh/varargs.h.html" class="external text external">"Single UNIX Specification <code>varargs.h</code>"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">2007-08-01</span></span>.</cite></span></li></ol></div></div>

<div role="navigation" class="navbox" aria-labelledby="C_programming_language" style="padding:3px"><table class="nowraplinks hlist mw-collapsible expanded navbox-inner" style="border-spacing:0;background:transparent;color:inherit"><tbody><tr><th scope="col" class="navbox-title" colspan="2"><div id="C_programming_language" style="font-size:114%;margin:0 4em"><a href="C_(programming_language)" title="C (programming language)">C programming language</a></div></th></tr><tr><td class="navbox-abovebelow" colspan="2"><div id="*ANSI_C_*C99_*C11_*C17_*C2x_*Embedded_C_*MISRA_C">
<ul><li><a href="ANSI_C" title="ANSI C">ANSI C</a></li>
<li><a href="C99" title="C99">C99</a></li>
<li><a href="C11_(C_standard_revision)" title="C11 (C standard revision)">C11</a></li>
<li><a href="C17_(C_standard_revision)" title="C17 (C standard revision)">C17</a></li>
<li><a href="C2x" title="C2x">C2x</a></li>
<li><a href="Embedded_C" title="Embedded C">Embedded C</a></li>
<li><a href="MISRA_C" title="MISRA C">MISRA C</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Features</th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="Subroutine" title="Subroutine">Functions</a></li>
<li><a href="Include_directive#C" title="Include directive">Header files</a></li>
<li><a href="Operators_in_C_and_C%2B%2B" title="Operators in C and C++">Operators</a></li>
<li><a href="C_string_handling" title="C string handling">String</a></li>
<li><a href="C_syntax" title="C syntax">Syntax</a></li>
<li><a href="C_preprocessor" title="C preprocessor">Preprocessor</a></li>
<li><a href="C_data_types" title="C data types">Data types</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%"><a href="C_standard_library" title="C standard library">Standard library</a></th><td class="navbox-list navbox-even" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="C_character_classification" title="C character classification">Char</a></li>
<li><a href="C_file_input/output" title="C file input/output">File I/O</a></li>
<li><a href="C_mathematical_functions" title="C mathematical functions">Math</a></li>
<li><a href="C_dynamic_memory_allocation" title="C dynamic memory allocation">Dynamic memory</a></li>
<li><a href="C_string_handling" title="C string handling">String</a></li>
<li><a href="C_date_and_time_functions" title="C date and time functions">Time</a></li>
<li><a href="Stdarg.h" title="Stdarg.h">Variadic</a></li>
<li><a href="C_POSIX_library" title="C POSIX library">POSIX</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Standard library<br>implementations</th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="Bionic_(software)" title="Bionic (software)">Bionic</a>
<ul><li><a href="Hybris_(software)" title="Hybris (software)">libhybris</a></li></ul></li>
<li><a href="Dietlibc" title="Dietlibc">dietlibc</a></li>
<li><a href="GNU_C_Library" title="GNU C Library">glibc</a>
<ul><li><a href="Embedded_GLIBC" title="Embedded GLIBC">EGLIBC</a></li></ul></li>
<li><a href="Klibc" title="Klibc">klibc</a></li>
<li><a href="Microsoft_Windows_library_files" title="Microsoft Windows library files">Windows CRT</a></li>
<li><a href="Musl" title="Musl">musl</a></li>
<li><a href="Newlib" title="Newlib">Newlib</a></li>
<li><a href="UClibc" title="UClibc">uClibc</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Compilers</th><td class="navbox-list navbox-even" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="Amsterdam_Compiler_Kit" title="Amsterdam Compiler Kit">ACK</a></li>
<li><a href="Borland_Turbo_C" title="Borland Turbo C">Borland Turbo C</a></li>
<li><a href="Clang" title="Clang">Clang</a></li>
<li><a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a></li>
<li><a href="Intel_C%2B%2B_Compiler" title="Intel C++ Compiler">ICC</a></li>
<li><a href="LCC_(compiler)" title="LCC (compiler)">LCC</a></li>
<li><a href="Norcroft_C_compiler" title="Norcroft C compiler">Norcroft C</a></li>
<li><a href="Portable_C_Compiler" title="Portable C Compiler">PCC</a></li>
<li><a href="Small_Device_C_Compiler" title="Small Device C Compiler">SDCC</a></li>
<li><a href="Tiny_C_Compiler" title="Tiny C Compiler">TCC</a></li>
<li><a href="Microsoft_Visual_Studio" title="Microsoft Visual Studio">Microsoft Visual Studio</a> / <a href="Microsoft_Visual_Studio_Express" title="Microsoft Visual Studio Express">Express</a> / <a href="Microsoft_Visual_C%2B%2B" title="Microsoft Visual C++">C++</a></li>
<li><a href="Watcom_C/C%2B%2B" title="Watcom C/C++">Watcom C/C++</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">IDEs</th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="Anjuta" title="Anjuta">Anjuta</a></li>
<li><a href="CLion" title="CLion" class="mw-redirect">CLion</a></li>
<li><a href="Code%3A%3ABlocks" title="Code::Blocks">Code::Blocks</a></li>
<li><a href="CodeLite" title="CodeLite">CodeLite</a></li>
<li><a href="Eclipse_(software)" title="Eclipse (software)">Eclipse</a></li>
<li><a href="Geany" title="Geany">Geany</a></li>
<li><a href="GNOME_Builder" title="GNOME Builder">GNOME Builder</a></li>
<li><a href="Microsoft_Visual_Studio" title="Microsoft Visual Studio">Microsoft Visual Studio</a></li>
<li><a href="NetBeans" title="NetBeans">NetBeans</a></li>
<li><a href="KDevelop" title="KDevelop">KDevelop</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Comparison with<br>other languages</th><td class="navbox-list navbox-even" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="Compatibility_of_C_and_C%2B%2B" title="Compatibility of C and C++">Compatibility of C and C++</a></li>
<li><a href="Comparison_of_Pascal_and_C" title="Comparison of Pascal and C">Comparison with Pascal</a></li></ul>
</div></td></tr><tr><th scope="row" class="navbox-group" style="width:1%">Descendant<br>languages</th><td class="navbox-list navbox-odd" style="text-align:left;border-left-width:2px;border-left-style:solid;width:100%;padding:0px"><div style="padding:0em 0.25em">
<ul><li><a href="C%2B%2B" title="C++">C++</a></li>
<li><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a></li>
<li><a href="D_(programming_language)" title="D (programming language)">D</a></li>
<li><a href="Objective-C" title="Objective-C">Objective-C</a></li>
<li><a href="Alef_(programming_language)" title="Alef (programming language)">Alef</a></li>
<li><a href="Limbo_(programming_language)" title="Limbo (programming language)">Limbo</a></li>
<li><a href="Go_(programming_language)" title="Go (programming language)">Go</a></li>
<li><a href="Vala_(programming_language)" title="Vala (programming language)">Vala</a></li></ul>
</div></td></tr><tr><td class="navbox-abovebelow" colspan="2"><div>
<ul><li><span><span><img src="../I/Folder_Hexagonal_Icon.svg.png.webp" data-file-width="36" data-file-height="31" data-file-type="drawing" height="14" width="16" loading="lazy"></span></span> <b>Category</b></li></ul>
</div></td></tr></tbody></table></div>



    
</details><!--htdig_noindex--><div><div style="clear:both; background-image:linear-gradient(180deg, #E8E8E8, white); border-top: dashed 2px #AAAAAA; padding: 0.5em 0.5em 0.5em 0.5em; margin-top: 1em; direction: ltr;">
    This article is issued from <a class="external text" title="Last edited on 2021-01-04" href="https://en.wikipedia.org/wiki/?title=Stdarg.h&amp;oldid=998152169">Wikipedia</a>. The text is licensed under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons - Attribution - Sharealike</a>. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
        </div>
      </div>
    </div>
  </div>
  <script src="../-/mw/jsConfigVars.js"></script>
  <script src="../-/mw/startup.js"></script>
<script src="../-/mw/jquery.js"></script>
<script src="../-/mw/mediawiki.js"></script>
<script src="../-/mw/site.js"></script>
<script src="../-/mw/ext.cite.ux-enhancements.js"></script>



</body></html>