xref: /freebsd/share/man/man3/stdarg.3 (revision 4ad443a106d3201453dc4ce0412431df2c0d1a47)
1afe61c15SRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
2afe61c15SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
3afe61c15SRodney W. Grimes.\"
4afe61c15SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
5afe61c15SRodney W. Grimes.\" the American National Standards Committee X3, on Information
6afe61c15SRodney W. Grimes.\" Processing Systems.
7afe61c15SRodney W. Grimes.\"
8afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
9afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions
10afe61c15SRodney W. Grimes.\" are met:
11afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
12afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
13afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
14afe61c15SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
15afe61c15SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
16dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors
17afe61c15SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
18afe61c15SRodney W. Grimes.\"    without specific prior written permission.
19afe61c15SRodney W. Grimes.\"
20afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23afe61c15SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30afe61c15SRodney W. Grimes.\" SUCH DAMAGE.
31afe61c15SRodney W. Grimes.\"
32*4ad443a1SJohn-Mark Gurney.Dd October 18, 2024
33afe61c15SRodney W. Grimes.Dt STDARG 3
34afe61c15SRodney W. Grimes.Os
35afe61c15SRodney W. Grimes.Sh NAME
36afe61c15SRodney W. Grimes.Nm stdarg
37afe61c15SRodney W. Grimes.Nd variable argument lists
38afe61c15SRodney W. Grimes.Sh SYNOPSIS
3932eef9aeSRuslan Ermilov.In stdarg.h
40afe61c15SRodney W. Grimes.Ft void
41afe61c15SRodney W. Grimes.Fn va_start "va_list ap" last
42afe61c15SRodney W. Grimes.Ft type
43afe61c15SRodney W. Grimes.Fn va_arg "va_list ap" type
44afe61c15SRodney W. Grimes.Ft void
4586954511SMike Barcroft.Fn va_copy "va_list dest" "va_list src"
4686954511SMike Barcroft.Ft void
47afe61c15SRodney W. Grimes.Fn va_end "va_list ap"
48afe61c15SRodney W. Grimes.Sh DESCRIPTION
49afe61c15SRodney W. GrimesA function may be called with a varying number of arguments of varying
50afe61c15SRodney W. Grimestypes.
51afe61c15SRodney W. GrimesThe include file
52fe08efe6SRuslan Ermilov.In stdarg.h
53afe61c15SRodney W. Grimesdeclares a type
54afe61c15SRodney W. Grimes.Pq Em va_list
55484e5bddSJohn Baldwinand defines four macros for stepping
56afe61c15SRodney W. Grimesthrough a list of arguments whose number and types are not known to
57afe61c15SRodney W. Grimesthe called function.
58afe61c15SRodney W. Grimes.Pp
59afe61c15SRodney W. GrimesThe called function must declare an object of type
60afe61c15SRodney W. Grimes.Em va_list
61afe61c15SRodney W. Grimeswhich is used by the macros
62afe61c15SRodney W. Grimes.Fn va_start ,
63afe61c15SRodney W. Grimes.Fn va_arg ,
64a40c540fSTim J. Robbins.Fn va_copy ,
65afe61c15SRodney W. Grimesand
66afe61c15SRodney W. Grimes.Fn va_end .
67afe61c15SRodney W. Grimes.Pp
68afe61c15SRodney W. GrimesThe
69afe61c15SRodney W. Grimes.Fn va_start
70afe61c15SRodney W. Grimesmacro initializes
71afe61c15SRodney W. Grimes.Fa ap
72afe61c15SRodney W. Grimesfor subsequent use by
73484e5bddSJohn Baldwin.Fn va_arg ,
74484e5bddSJohn Baldwin.Fn va_copy ,
75afe61c15SRodney W. Grimesand
76afe61c15SRodney W. Grimes.Fn va_end ,
77afe61c15SRodney W. Grimesand must be called first.
78afe61c15SRodney W. Grimes.Pp
79afe61c15SRodney W. GrimesThe parameter
80afe61c15SRodney W. Grimes.Fa last
81afe61c15SRodney W. Grimesis the name of the last parameter before the variable argument list,
825203edcdSRuslan Ermilovi.e., the last parameter of which the calling function knows the type.
83afe61c15SRodney W. Grimes.Pp
84afe61c15SRodney W. GrimesBecause the address of this parameter is used in the
85afe61c15SRodney W. Grimes.Fn va_start
86afe61c15SRodney W. Grimesmacro, it should not be declared as a register variable, or as a
87afe61c15SRodney W. Grimesfunction or an array type.
88afe61c15SRodney W. Grimes.Pp
89afe61c15SRodney W. GrimesThe
90afe61c15SRodney W. Grimes.Fn va_arg
91afe61c15SRodney W. Grimesmacro expands to an expression that has the type and value of the next
92afe61c15SRodney W. Grimesargument in the call.
93afe61c15SRodney W. GrimesThe parameter
94afe61c15SRodney W. Grimes.Fa ap
95afe61c15SRodney W. Grimesis the
96afe61c15SRodney W. Grimes.Em va_list Fa ap
97afe61c15SRodney W. Grimesinitialized by
98484e5bddSJohn Baldwin.Fn va_start
99484e5bddSJohn Baldwinor
100484e5bddSJohn Baldwin.Fn va_copy .
101afe61c15SRodney W. GrimesEach call to
102afe61c15SRodney W. Grimes.Fn va_arg
103afe61c15SRodney W. Grimesmodifies
104afe61c15SRodney W. Grimes.Fa ap
105afe61c15SRodney W. Grimesso that the next call returns the next argument.
106afe61c15SRodney W. GrimesThe parameter
107afe61c15SRodney W. Grimes.Fa type
108afe61c15SRodney W. Grimesis a type name specified so that the type of a pointer to an
109afe61c15SRodney W. Grimesobject that has the specified type can be obtained simply by
110afe61c15SRodney W. Grimesadding a *
111afe61c15SRodney W. Grimesto
112afe61c15SRodney W. Grimes.Fa type .
113afe61c15SRodney W. Grimes.Pp
114afe61c15SRodney W. GrimesIf there is no next argument, or if
115afe61c15SRodney W. Grimes.Fa type
116afe61c15SRodney W. Grimesis not compatible with the type of the actual next argument
117afe61c15SRodney W. Grimes(as promoted according to the default argument promotions),
118afe61c15SRodney W. Grimesrandom errors will occur.
119afe61c15SRodney W. Grimes.Pp
120afe61c15SRodney W. GrimesThe first use of the
121afe61c15SRodney W. Grimes.Fn va_arg
122afe61c15SRodney W. Grimesmacro after that of the
123afe61c15SRodney W. Grimes.Fn va_start
124afe61c15SRodney W. Grimesmacro returns the argument after
125afe61c15SRodney W. Grimes.Fa last .
126afe61c15SRodney W. GrimesSuccessive invocations return the values of the remaining
127afe61c15SRodney W. Grimesarguments.
128afe61c15SRodney W. Grimes.Pp
129afe61c15SRodney W. GrimesThe
13086954511SMike Barcroft.Fn va_copy
13186954511SMike Barcroftmacro copies a variable argument list, previously initialized by
13286954511SMike Barcroft.Fn va_start ,
13386954511SMike Barcroftfrom
134d962d52aSRuslan Ermilov.Fa src
13586954511SMike Barcroftto
136d962d52aSRuslan Ermilov.Fa dest .
13786954511SMike BarcroftThe state is preserved such that it is equivalent to calling
13886954511SMike Barcroft.Fn va_start
13986954511SMike Barcroftwith the same second argument used with
140d962d52aSRuslan Ermilov.Fa src ,
14186954511SMike Barcroftand calling
14286954511SMike Barcroft.Fn va_arg
14386954511SMike Barcroftthe same number of times as called with
144d962d52aSRuslan Ermilov.Fa src .
14586954511SMike Barcroft.Pp
14686954511SMike BarcroftThe
147484e5bddSJohn Baldwin.Fn va_end
148484e5bddSJohn Baldwinmacro cleans up any state associated with the variable argument list
149484e5bddSJohn Baldwin.Fa ap .
150484e5bddSJohn Baldwin.Pp
151484e5bddSJohn BaldwinEach invocation of
152484e5bddSJohn Baldwin.Fn va_start
153484e5bddSJohn Baldwinor
15486954511SMike Barcroft.Fn va_copy
155484e5bddSJohn Baldwinmust be paired with a corresponding invocation of
156484e5bddSJohn Baldwin.Fn va_end
157484e5bddSJohn Baldwinin the same function.
158484e5bddSJohn Baldwin.Sh RETURN VALUES
159484e5bddSJohn BaldwinThe
160484e5bddSJohn Baldwin.Fn va_arg
161484e5bddSJohn Baldwinmacro returns the value of the next argument.
16286954511SMike Barcroft.Pp
16386954511SMike BarcroftThe
164484e5bddSJohn Baldwin.Fn va_start ,
165484e5bddSJohn Baldwin.Fn va_copy ,
166484e5bddSJohn Baldwinand
167afe61c15SRodney W. Grimes.Fn va_end
168484e5bddSJohn Baldwinmacros return no value.
169afe61c15SRodney W. Grimes.Sh EXAMPLES
170afe61c15SRodney W. GrimesThe function
171afe61c15SRodney W. Grimes.Em foo
172afe61c15SRodney W. Grimestakes a string of format characters and prints out the argument
173afe61c15SRodney W. Grimesassociated with each format character based on the type.
174afe61c15SRodney W. Grimes.Bd -literal -offset indent
175afe61c15SRodney W. Grimesvoid foo(char *fmt, ...)
176afe61c15SRodney W. Grimes{
177afe61c15SRodney W. Grimes	va_list ap;
178afe61c15SRodney W. Grimes	int d;
179cc023582SDima Dorfman	char c, *s;
180afe61c15SRodney W. Grimes
181afe61c15SRodney W. Grimes	va_start(ap, fmt);
182afe61c15SRodney W. Grimes	while (*fmt)
183afe61c15SRodney W. Grimes		switch(*fmt++) {
184afe61c15SRodney W. Grimes		case 's':			/* string */
185afe61c15SRodney W. Grimes			s = va_arg(ap, char *);
186afe61c15SRodney W. Grimes			printf("string %s\en", s);
187afe61c15SRodney W. Grimes			break;
188afe61c15SRodney W. Grimes		case 'd':			/* int */
189afe61c15SRodney W. Grimes			d = va_arg(ap, int);
190afe61c15SRodney W. Grimes			printf("int %d\en", d);
191afe61c15SRodney W. Grimes			break;
192afe61c15SRodney W. Grimes		case 'c':			/* char */
19304bbb62bSJoshua Peck Macdonald			/* Note: char is promoted to int. */
19404bbb62bSJoshua Peck Macdonald			c = va_arg(ap, int);
195afe61c15SRodney W. Grimes			printf("char %c\en", c);
196afe61c15SRodney W. Grimes			break;
197afe61c15SRodney W. Grimes		}
198afe61c15SRodney W. Grimes	va_end(ap);
199afe61c15SRodney W. Grimes}
200afe61c15SRodney W. Grimes.Ed
2019cbda590SRuslan Ermilov.Sh COMPATIBILITY
2029cbda590SRuslan ErmilovThese macros are
2039cbda590SRuslan Ermilov.Em not
2049cbda590SRuslan Ermilovcompatible with the historic macros they replace.
205afe61c15SRodney W. Grimes.Sh STANDARDS
206afe61c15SRodney W. GrimesThe
207afe61c15SRodney W. Grimes.Fn va_start ,
208afe61c15SRodney W. Grimes.Fn va_arg ,
209a40c540fSTim J. Robbins.Fn va_copy ,
210afe61c15SRodney W. Grimesand
211afe61c15SRodney W. Grimes.Fn va_end
212afe61c15SRodney W. Grimesmacros conform to
213a40c540fSTim J. Robbins.St -isoC-99 .
214757a04bfSSergio Carlavilla Delgado.Sh HISTORY
215757a04bfSSergio Carlavilla DelgadoThe
216757a04bfSSergio Carlavilla Delgado.Fn va_start ,
217757a04bfSSergio Carlavilla Delgado.Fn va_arg
218757a04bfSSergio Carlavilla Delgadoand
219757a04bfSSergio Carlavilla Delgado.Fn va_end
220757a04bfSSergio Carlavilla Delgadomacros were introduced in
221757a04bfSSergio Carlavilla Delgado.St -ansiC .
222757a04bfSSergio Carlavilla DelgadoThe
223757a04bfSSergio Carlavilla Delgado.Fn va_copy
224757a04bfSSergio Carlavilla Delgadomacro was introduced in
225757a04bfSSergio Carlavilla Delgado.St -isoC-99 .
226afe61c15SRodney W. Grimes.Sh BUGS
227afe61c15SRodney W. GrimesUnlike the
228afe61c15SRodney W. Grimes.Em varargs
229afe61c15SRodney W. Grimesmacros, the
2304b66483fSRuslan Ermilov.Nm
231afe61c15SRodney W. Grimesmacros do not permit programmers to
232afe61c15SRodney W. Grimescode a function with no fixed arguments.
233afe61c15SRodney W. GrimesThis problem generates work mainly when converting
234afe61c15SRodney W. Grimes.Em varargs
235afe61c15SRodney W. Grimescode to
2364b66483fSRuslan Ermilov.Nm
237afe61c15SRodney W. Grimescode,
238afe61c15SRodney W. Grimesbut it also creates difficulties for variadic functions that
239afe61c15SRodney W. Grimeswish to pass all of their arguments on to a function
240afe61c15SRodney W. Grimesthat takes a
241afe61c15SRodney W. Grimes.Em va_list
242afe61c15SRodney W. Grimesargument, such as
243afe61c15SRodney W. Grimes.Xr vfprintf 3 .
244