1STDARG(3)                  Linux Programmer's Manual                 STDARG(3)
2
3
4

NAME

6       stdarg - variable argument lists
7

SYNOPSIS

9       #include <stdarg.h>
10
11       void va_start(va_list ap, last);
12       type va_arg(va_list ap, type);
13       void va_end(va_list ap);
14       void va_copy(va_list dest, va_list src);
15

DESCRIPTION

17       A  function may be called with a varying number of arguments of varying
18       types.  The include file <stdarg.h> declares a type va_list and defines
19       three  macros for stepping through a list of arguments whose number and
20       types are not known to the called function.
21
22       The called function must declare an object of  type  va_list  which  is
23       used by the macros va_start(), va_arg(), and va_end().
24
25   va_start
26       The  va_start() macro initializes ap for subsequent use by va_arg() and
27       va_end(), and must be called first.
28
29       The parameter last is the name of the last parameter before  the  vari‐
30       able argument list, i.e., the last parameter of which the calling func‐
31       tion knows the type.
32
33       Because the address of this parameter may be  used  in  the  va_start()
34       macro,  it should not be declared as a register variable, or as a func‐
35       tion or an array type.
36
37   va_arg
38       The va_arg() macro expands to an expression that has the type and value
39       of  the  next argument in the call.  The parameter ap is the va_list ap
40       initialized by va_start().  Each call to va_arg() modifies ap  so  that
41       the  next call returns the next argument.  The parameter type is a type
42       name specified so that the type of a pointer to an object that has  the
43       specified type can be obtained simply by adding a * to type.
44
45       The  first use of the va_arg() macro after that of the va_start() macro
46       returns the argument after last.   Successive  invocations  return  the
47       values of the remaining arguments.
48
49       If  there  is  no  next argument, or if type is not compatible with the
50       type of the actual next argument (as promoted according to the  default
51       argument promotions), random errors will occur.
52
53       If  ap is passed to a function that uses va_arg(ap,type) then the value
54       of ap is undefined after the return of that function.
55
56   va_end
57       Each invocation of va_start() must be matched by a corresponding  invo‐
58       cation  of va_end() in the same function. After the call va_end(ap) the
59       variable ap is undefined.  Multiple  transversals  of  the  list,  each
60       bracketed  by  va_start() and va_end() are possible.  va_end() may be a
61       macro or a function.
62
63   va_copy
64       An obvious implementation would have a va_list  be  a  pointer  to  the
65       stack frame of the variadic function.  In such a setup (by far the most
66       common) there seems nothing against an assignment
67                   va_list aq = ap;
68       Unfortunately, there are also systems that make it an array of pointers
69       (of length 1), and there one needs
70                   va_list aq;
71                   *aq = *ap;
72       Finally, on systems where parameters are passed in registers, it may be
73       necessary for va_start()  to  allocate  memory,  store  the  parameters
74       there,  and  also  an  indication  of  which parameter is next, so that
75       va_arg() can step through the list. Now va_end() can free the allocated
76       memory  again.   To  accommodate  this  situation,  C99  adds  a  macro
77       va_copy(), so that the above assignment can be replaced by
78                   va_list aq;
79                   va_copy(aq, ap);
80                   ...
81                   va_end(aq);
82       Each invocation of va_copy() must be matched by a corresponding invoca‐
83       tion of va_end() in the same function.  Some systems that do not supply
84       va_copy() have __va_copy instead, since that was the name used  in  the
85       draft proposal.
86

EXAMPLE

88       The function foo takes a string of format characters and prints out the
89       argument associated with each format character based on the type.
90              #include <stdio.h>
91              #include <stdarg.h>
92
93              void foo(char *fmt, ...) {
94                   va_list ap;
95                   int d;
96                   char c, *s;
97
98                   va_start(ap, fmt);
99                   while (*fmt)
100                        switch(*fmt++) {
101                        case 's':           /* string */
102                             s = va_arg(ap, char *);
103                             printf("string %s\n", s);
104                             break;
105                        case 'd':           /* int */
106                             d = va_arg(ap, int);
107                             printf("int %d\n", d);
108                             break;
109                        case 'c':           /* char */
110                             /* need a cast here since va_arg only
111                                takes fully promoted types */
112                             c = (char) va_arg(ap, int);
113                             printf("char %c\n", c);
114                             break;
115                        }
116                   va_end(ap);
117              }
118

CONFORMING TO

120       The va_start(), va_arg(), and va_end()  macros  conform  to  C89.   C99
121       defines the va_copy() macro.
122

COMPATIBILITY

124       These  macros are not compatible with the historic macros they replace.
125       A backward  compatible  version  can  be  found  in  the  include  file
126       varargs.h.
127

COMPARISON

129       The historic setup is:
130              #include <varargs.h>
131
132              void foo(va_alist) va_dcl {
133                   va_list ap;
134
135                   va_start(ap);
136                   while(...) {
137                        ...
138                        x = va_arg(ap, type);
139                        ...
140                   }
141                   va_end(ap);
142              }
143       On  some  systems,  va_end  contains  a  closing  '}' matching a '{' in
144       va_start, so that both macros must occur in the same function, and in a
145       way that allows this.
146

BUGS

148       Unlike  the varargs macros, the stdarg macros do not permit programmers
149       to code a function with no fixed  arguments.   This  problem  generates
150       work  mainly  when  converting varargs code to stdarg code, but it also
151       creates difficulties for variadic functions that wish to  pass  all  of
152       their arguments on to a function that takes a va_list argument, such as
153       vfprintf(3).
154
155
156
157                                  2001-10-14                         STDARG(3)
Impressum