BSD 4_3_Tahoe release
[unix-history] / usr / src / man / man3 / varargs.3
CommitLineData
95f51977 1.\" @(#)varargs.3 6.3 (Berkeley) 5/15/86
41a44cfa 2.\"
95f51977 3.TH VARARGS 3 "May 15, 1986"
41a44cfa
KM
4.AT 3
5.SH NAME
6varargs \- variable argument list
7.SH SYNOPSIS
4d575989
KM
8.B "#include <varargs.h>"
9.PP
10.I function\c
11.RB ( va_alist )
12.br
13.B va_dcl
14.br
15.B va_list
16.IR pvar ;
17.br
18.B va_start\c
19.RI ( pvar );
20.br
21f =
22.B va_arg\c
23.RI ( pvar ,
24.IR type );
25.br
26.B va_end\c
27.RI ( pvar );
41a44cfa 28.SH DESCRIPTION
4d575989
KM
29This set of macros provides a means of writing portable procedures that
30accept variable argument lists.
31Routines having variable argument lists (such as
32.IR printf (3))
41a44cfa
KM
33that do not use varargs are inherently nonportable, since different
34machines use different argument passing conventions.
35.PP
36.B va_alist
37is used in a function header to declare a variable argument list.
38.PP
39.B va_dcl
4d575989
KM
40is a declaration for
41.BR va_alist .
41a44cfa
KM
42Note that there is no semicolon after
43.B va_dcl.
44.PP
45.B va_list
46is a type which can be used for the variable
4d575989 47.IR pvar ,
41a44cfa
KM
48which is used to traverse the list.
49One such variable must always be declared.
50.PP
4d575989 51.B va_start\c
41a44cfa
KM
52.RI (pvar)
53is called to initialize
54.I pvar
55to the beginning of the list.
56.PP
4d575989
KM
57.B va_arg\c
58.RI ( pvar ,
59.IR type )
60will return the next argument in the list pointed to by
41a44cfa
KM
61.IR pvar .
62.I Type
ffb572e8
MK
63is the type to which the expected argument will be converted
64when passed as an argument.
65In standard C, arguments that are
66.B char
67or
68.B short
69should be accessed as
70.BR int ,
71.B "unsigned char
72or
73.B "unsigned short
74are converted to
75.BR "unsigned int" ,
76and
77.B float
78arguments are converted to
79.BR double .
41a44cfa
KM
80Different types can be mixed, but it is up
81to the routine to know what type of argument is
82expected, since it cannot be determined at runtime.
83.PP
4d575989 84.B va_end\c
41a44cfa
KM
85.RI ( pvar )
86is used to finish up.
87.PP
4d575989 88Multiple traversals, each bracketed by
41a44cfa 89.B va_start
4d575989 90\&...
41a44cfa
KM
91.B va_end,
92are possible.
93.SH EXAMPLE
94.nf
95 \fB#include\fP <varargs.h>
96 execl(\fBva_alist\fP)
97 \fBva_dcl\fP
98 {
99 \fBva_list\fP ap;
100 \fBchar\fP *file;
101 \fBchar\fP *args[100];
102 \fBint\fP argno = 0;
103
104 \fBva_start\fP(ap);
105 file = \fBva_arg(ap, \fBchar\fP *);
106 \fBwhile\fP (args[argno++] = \fBva_arg\fP(ap, \fBchar\fP *))
107 \fB;\fP
108 \fBva_end\fP(ap);
109 \fBreturn\fP execv(file, args);
110 }
111.fi
112.SH BUGS
113It is up to the calling routine to determine how many arguments
114there are, since it is not possible to determine this from the
115stack frame. For example,
116.I execl
117passes a 0 to signal the end of the list.
118.I Printf
4d575989 119can tell how many arguments are supposed to be there by the format.
ffb572e8
MK
120.PP
121The macros
122.I va_start
123and
124.I va_end
a610f18c
MK
125may be arbitrarily complex;
126for example,
127.I va_start
128might contain an opening brace,
129which is closed by a matching brace in
130.IR va_end .
131Thus, they should only be used where they could
ffb572e8 132be placed within a single complex statement.