1VARARGS(3) Library Functions Manual VARARGS(3)
2
3
4
6 varargs - variable argument list
7
9 #include <varargs.h>
10
11 function(va_alist)
12 va_dcl
13 va_list pvar;
14 va_start(pvar);
15 f = va_arg(pvar, type);
16 va_end(pvar);
17
19 This set of macros provides a means of writing portable procedures that
20 accept variable argument lists. Routines having variable argument
21 lists (such as printf(3)) that do not use varargs are inherently non‐
22 portable, since different machines use different argument passing con‐
23 ventions.
24
25 va_alist is used in a function header to declare a variable argument
26 list.
27
28 va_dcl is a declaration for va_alist. Note that there is no semicolon
29 after va_dcl.
30
31 va_list is a type which can be used for the variable pvar, which is
32 used to traverse the list. One such variable must always be declared.
33
34 va_start(pvar) is called to initialize pvar to the beginning of the
35 list.
36
37 va_arg(pvar, type) will return the next argument in the list pointed to
38 by pvar. Type is the type to which the expected argument will be con‐
39 verted when passed as an argument. In standard C, arguments that are
40 char or short should be accessed as int, unsigned char or unsigned
41 short are converted to unsigned int, and float arguments are converted
42 to double. Different types can be mixed, but it is up to the routine
43 to know what type of argument is expected, since it cannot be deter‐
44 mined at runtime.
45
46 va_end(pvar) is used to finish up.
47
48 Multiple traversals, each bracketed by va_start ... va_end, are possi‐
49 ble.
50
52 #include <varargs.h>
53 execl(va_alist)
54 va_dcl
55 {
56 va_list ap;
57 char *file;
58 char *args[100];
59 int argno = 0;
60
61 va_start(ap);
62 file = va_arg(ap, char *);
63 while (args[argno++] = va_arg(ap, char *))
64 ;
65 va_end(ap);
66 return execv(file, args);
67 }
68
70 It is up to the calling routine to determine how many arguments there
71 are, since it is not possible to determine this from the stack frame.
72 For example, execl passes a 0 to signal the end of the list. Printf
73 can tell how many arguments are supposed to be there by the format.
74
75 The macros va_start and va_end may be arbitrarily complex; for example,
76 va_start might contain an opening brace, which is closed by a matching
77 brace in va_end. Thus, they should only be used where they could be
78 placed within a single complex statement.
79
80
81
827th Edition May 15, 1986 VARARGS(3)