1varargs(3EXT) Extended Library Functions varargs(3EXT)
2
3
4
6 varargs - handle variable argument list
7
9 #include <varargs.h>
10 va_alist
11 va_dcl
12 va_list pvar;
13
14 void va_start(va_listpvar);
15
16
17 type va_arg(va_list pvar, type);
18
19
20 void va_end(va_list pvar);
21
22
24 This set of macros allows portable procedures that accept variable
25 argument lists to be written. Routines that have variable argument
26 lists (such as printf(3C)) but do not use varargs are inherently non-
27 portable, as different machines use different argument-passing conven‐
28 tions.
29
30
31 va_alist is used as the parameter list in a function header.
32
33
34 va_dcl is a declaration for va_alist. No semicolon should follow
35 va_dcl.
36
37
38 va_list is a type defined for the variable used to traverse the list.
39
40
41 va_start is called to initialize pvar to the beginning of the list.
42
43
44 va_arg will return the next argument in the list pointed to by pvar.
45 type is the type the argument is expected to be. Different types can be
46 mixed, but it is up to the routine to know what type of argument is
47 expected, as it cannot be determined at runtime.
48
49
50 va_end is used to clean up.
51
52
53 Multiple traversals, each bracketed by va_start and va_end, are possi‐
54 ble.
55
57 Example 1 A sample program.
58
59
60 This example is a possible implementation of execl (see exec(2) ).
61
62
63 #include <unistd.h>
64 #include <varargs.h>
65 #define MAXARGS 100
66 /* execl is called by
67 execl(file, arg1, arg2, ..., (char *)0);
68 */
69 execl(va_alist)
70 va_dcl
71 {
72 va_list ap;
73 char *file;
74 char *args[MAXARGS]; /* assumed big enough*/
75 int argno = 0;
76
77 va_start(ap);
78 file = va_arg(ap, char *);
79 while ((args[argno++] = va_arg(ap, char *)) != 0)
80 ;
81 va_end(ap);
82 return execv(file, args);
83 }
84
85
86
88 exec(2), printf(3C), vprintf(3C), stdarg(3EXT)
89
91 It is up to the calling routine to specify in some manner how many
92 arguments there are, since it is not always possible to determine the
93 number of arguments from the stack frame. For example, execl is passed
94 a zero pointer to signal the end of the list. printf can tell how many
95 arguments are there by the format.
96
97
98 It is non-portable to specify a second argument of char, short, or
99 float to va_arg, since arguments seen by the called function are not
100 char, short, or float. C converts char and short arguments to int and
101 converts float arguments to double before passing them to a function.
102
103
104 stdarg is the preferred interface.
105
106
107
108SunOS 5.11 10 May 2002 varargs(3EXT)