xref: /netbsd-src/share/man/man3/stdarg.3 (revision 3a11b350c07f0c8af663e911d33221cd3935cb9f)
1*3a11b350Sdholland.\"	$NetBSD: stdarg.3,v 1.21 2015/06/15 02:06:18 dholland Exp $
2ee52468aSjtc.\"
3ee52468aSjtc.\" Copyright (c) 1990, 1991, 1993
4ee52468aSjtc.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" This code is derived from software contributed to Berkeley by
761f28255Scgd.\" the American National Standards Committee X3, on Information
861f28255Scgd.\" Processing Systems.
961f28255Scgd.\"
1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without
1161f28255Scgd.\" modification, are permitted provided that the following conditions
1261f28255Scgd.\" are met:
1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1461f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd.\"    documentation and/or other materials provided with the distribution.
18075022b3Sagc.\" 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd.\"    may be used to endorse or promote products derived from this software
2061f28255Scgd.\"    without specific prior written permission.
2161f28255Scgd.\"
2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd.\" SUCH DAMAGE.
3361f28255Scgd.\"
34ee52468aSjtc.\"	@(#)stdarg.3	8.1 (Berkeley) 6/5/93
3561f28255Scgd.\"
36*3a11b350Sdholland.Dd June 14, 2015
3761f28255Scgd.Dt STDARG 3
3861f28255Scgd.Os
3961f28255Scgd.Sh NAME
406be07a67Smrg.Nm stdarg ,
416be07a67Smrg.Nm va_arg  ,
4272592c10Skleink.Nm va_copy  ,
436be07a67Smrg.Nm va_end ,
446be07a67Smrg.Nm va_start
4561f28255Scgd.Nd variable argument lists
4661f28255Scgd.Sh SYNOPSIS
47472351e1Swiz.In stdarg.h
4861f28255Scgd.Ft void
4961f28255Scgd.Fn va_start "va_list ap" last
5061f28255Scgd.Ft type
5161f28255Scgd.Fn va_arg "va_list ap" type
5261f28255Scgd.Ft void
5372592c10Skleink.Fn va_copy "va_list dest" "va_list src"
5472592c10Skleink.Ft void
5561f28255Scgd.Fn va_end "va_list ap"
5661f28255Scgd.Sh DESCRIPTION
5761f28255ScgdA function may be called with a varying number of arguments of varying
5861f28255Scgdtypes.
5961f28255ScgdThe include file
60165d9f9fSjoerg.In stdarg.h
6161f28255Scgddeclares a type
6261f28255Scgd.Pq Em va_list
6361f28255Scgdand defines three macros for stepping
6461f28255Scgdthrough a list of arguments whose number and types are not known to
6561f28255Scgdthe called function.
6661f28255Scgd.Pp
6761f28255ScgdThe called function must declare an object of type
6861f28255Scgd.Em va_list
6961f28255Scgdwhich is used by the macros
7061f28255Scgd.Fn va_start ,
7161f28255Scgd.Fn va_arg ,
7272592c10Skleink.Fn va_end ,
7372592c10Skleinkand, optionally,
7472592c10Skleink.Fn va_copy .
7561f28255Scgd.Pp
7661f28255ScgdThe
7761f28255Scgd.Fn va_start
7861f28255Scgdmacro initializes
7961f28255Scgd.Fa ap
8061f28255Scgdfor subsequent use by
8172592c10Skleink.Fn va_arg ,
8272592c10Skleink.Fn va_copy
8361f28255Scgdand
8461f28255Scgd.Fn va_end ,
8561f28255Scgdand must be called first.
8661f28255Scgd.Pp
8761f28255ScgdThe parameter
8861f28255Scgd.Fa last
8961f28255Scgdis the name of the last parameter before the variable argument list,
9061f28255Scgdi.e. the last parameter of which the calling function knows the type.
9161f28255Scgd.Pp
9261f28255ScgdBecause the address of this parameter is used in the
9361f28255Scgd.Fn va_start
9461f28255Scgdmacro, it should not be declared as a register variable, or as a
9561f28255Scgdfunction or an array type.
9661f28255Scgd.Pp
9761f28255ScgdThe
9861f28255Scgd.Fn va_start
9961f28255Scgdmacro returns no value.
10061f28255Scgd.Pp
10161f28255ScgdThe
10261f28255Scgd.Fn va_arg
10361f28255Scgdmacro expands to an expression that has the type and value of the next
10461f28255Scgdargument in the call.
10561f28255ScgdThe parameter
10661f28255Scgd.Fa ap
10761f28255Scgdis the
10861f28255Scgd.Em va_list Fa ap
10961f28255Scgdinitialized by
11061f28255Scgd.Fn va_start .
11161f28255ScgdEach call to
11261f28255Scgd.Fn va_arg
11361f28255Scgdmodifies
11461f28255Scgd.Fa ap
11561f28255Scgdso that the next call returns the next argument.
11661f28255ScgdThe parameter
11761f28255Scgd.Fa type
11861f28255Scgdis a type name specified so that the type of a pointer to an
11961f28255Scgdobject that has the specified type can be obtained simply by
12061f28255Scgdadding a *
12161f28255Scgdto
12261f28255Scgd.Fa type .
12361f28255Scgd.Pp
12461f28255ScgdIf there is no next argument, or if
12561f28255Scgd.Fa type
12661f28255Scgdis not compatible with the type of the actual next argument
12761f28255Scgd(as promoted according to the default argument promotions),
12861f28255Scgdrandom errors will occur.
12961f28255Scgd.Pp
130d43d55bcSyamtIf the type in question is one that gets promoted, the promoted type
131d43d55bcSyamtshould be used as the argument to
132d43d55bcSyamt.Fn va_arg .
133d43d55bcSyamtThe following describes which types are promoted (and to what):
134d43d55bcSyamt.Bl -dash -compact
135d43d55bcSyamt.It
136d43d55bcSyamt.Va short
137d43d55bcSyamtis promoted to
138d43d55bcSyamt.Va int
139d43d55bcSyamt.It
140d43d55bcSyamt.Va float
141d43d55bcSyamtis promoted to
142d43d55bcSyamt.Va double
143d43d55bcSyamt.It
144d43d55bcSyamt.Va char
145d43d55bcSyamtis promoted to
146d43d55bcSyamt.Va int
147d43d55bcSyamt.El
148d43d55bcSyamt.Pp
14961f28255ScgdThe first use of the
15061f28255Scgd.Fn va_arg
15161f28255Scgdmacro after that of the
15261f28255Scgd.Fn va_start
15361f28255Scgdmacro returns the argument after
15461f28255Scgd.Fa last .
15561f28255ScgdSuccessive invocations return the values of the remaining
15661f28255Scgdarguments.
15761f28255Scgd.Pp
15861f28255ScgdThe
15972592c10Skleink.Fn va_copy
16072592c10Skleinkmacro makes
16172592c10Skleink.Fa dest
16272592c10Skleinka copy of
16372592c10Skleink.Fa src
16472592c10Skleinkas if the
16572592c10Skleink.Fn va_start
16672592c10Skleinkmacro had been applied to it followed by the same sequence of uses of the
16772592c10Skleink.Fn va_arg
168c67e34c8Skleinkmacro as had previously been used to reach the present state of
16972592c10Skleink.Fa src .
17072592c10Skleink.Pp
17172592c10SkleinkThe
17272592c10Skleink.Fn va_copy
17372592c10Skleinkmacro returns no value.
17472592c10Skleink.Pp
17572592c10SkleinkThe
17661f28255Scgd.Fn va_end
17761f28255Scgdmacro handles a normal return from the function whose variable argument
17861f28255Scgdlist was initialized by
17972592c10Skleink.Fn va_start
18072592c10Skleinkor
18172592c10Skleink.Fn va_copy .
18261f28255Scgd.Pp
18361f28255ScgdThe
18461f28255Scgd.Fn va_end
18561f28255Scgdmacro returns no value.
18661f28255Scgd.Sh EXAMPLES
18761f28255ScgdThe function
188d43d55bcSyamt.Fn foo
18961f28255Scgdtakes a string of format characters and prints out the argument
19061f28255Scgdassociated with each format character based on the type.
19161f28255Scgd.Bd -literal -offset indent
192d43d55bcSyamtvoid
193d43d55bcSyamtfoo(char *fmt, ...)
19461f28255Scgd{
19561f28255Scgd	va_list ap;
196d43d55bcSyamt	int d, c;
197d43d55bcSyamt	char *s;
198d43d55bcSyamt	double f;
19961f28255Scgd
20061f28255Scgd	va_start(ap, fmt);
201d43d55bcSyamt	while (*fmt)
20261f28255Scgd		switch (*fmt++) {
20361f28255Scgd		case 's':			/* string */
20461f28255Scgd			s = va_arg(ap, char *);
20561f28255Scgd			printf("string %s\en", s);
20661f28255Scgd			break;
20761f28255Scgd		case 'd':			/* int */
20861f28255Scgd			d = va_arg(ap, int);
20961f28255Scgd			printf("int %d\en", d);
21061f28255Scgd			break;
21161f28255Scgd		case 'c':			/* char */
212d43d55bcSyamt			c = va_arg(ap, int);	/* promoted */
21361f28255Scgd			printf("char %c\en", c);
21461f28255Scgd			break;
215d43d55bcSyamt		case 'f':			/* float */
216d43d55bcSyamt			f = va_arg(ap, double); /* promoted */
217d43d55bcSyamt			printf("float %f\en", f);
218e930ad8aSkleink		}
21961f28255Scgd	va_end(ap);
22061f28255Scgd}
22161f28255Scgd.Ed
2220c99d89eSwiz.Sh COMPATIBILITY
2230c99d89eSwizThese macros are
2240c99d89eSwiz.Em not
225*3a11b350Sdhollandcompatible with the historic
226*3a11b350Sdholland.In varargs.h
227*3a11b350Sdhollandmacros they replaced.
228*3a11b350SdhollandAny remaining code using the pre-C89
229*3a11b350Sdholland.In varargs.h
230*3a11b350Sdhollandinterface should be updated.
23161f28255Scgd.Sh STANDARDS
23261f28255ScgdThe
23361f28255Scgd.Fn va_start ,
23461f28255Scgd.Fn va_arg ,
235468efa58Skleink.Fn va_copy ,
23661f28255Scgdand
23761f28255Scgd.Fn va_end
23861f28255Scgdmacros conform to
239dca9ccd3Swiz.St -isoC-99 .
240468efa58Skleink.Sh HISTORY
241468efa58SkleinkThe
242468efa58Skleink.Fn va_start ,
243468efa58Skleink.Fn va_arg
244468efa58Skleinkand
245468efa58Skleink.Fn va_end
246468efa58Skleinkmacros were introduced in
2471b7ae2b7Skleink.St -ansiC .
248468efa58SkleinkThe
249468efa58Skleink.Fn va_copy
250468efa58Skleinkmacro was introduced in
251dca9ccd3Swiz.St -isoC-99 .
25261f28255Scgd.Sh BUGS
25361f28255ScgdUnlike the
25461f28255Scgd.Em varargs
25561f28255Scgdmacros, the
25661f28255Scgd.Nm stdarg
25761f28255Scgdmacros do not permit programmers to
25861f28255Scgdcode a function with no fixed arguments.
25961f28255ScgdThis problem generates work mainly when converting
26061f28255Scgd.Em varargs
26161f28255Scgdcode to
26261f28255Scgd.Nm stdarg
26361f28255Scgdcode,
26461f28255Scgdbut it also creates difficulties for variadic functions that
26561f28255Scgdwish to pass all of their arguments on to a function
26661f28255Scgdthat takes a
26761f28255Scgd.Em va_list
26861f28255Scgdargument, such as
26961f28255Scgd.Xr vfprintf 3 .
270