BSD 4_3_Reno release
[unix-history] / usr / src / share / man / man3 / varargs.3
.\" Copyright (c) 1990 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms are permitted
.\" provided that: (1) source distributions retain this entire copyright
.\" notice and comment, and (2) distributions including binaries display
.\" the following acknowledgement: ``This product includes software
.\" developed by the University of California, Berkeley and its contributors''
.\" in the documentation or other materials provided with the distribution
.\" and in all advertising materials mentioning features or use of this
.\" software. Neither the name of the University nor the names of its
.\" contributors may be used to endorse or promote products derived
.\" from this software without specific prior written permission.
.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
.\"
.\" @(#)varargs.3 6.4 (Berkeley) 5/14/90
.\"
.TH VARARGS 3 "May 14, 1990"
.AT 3
.SH NAME
varargs \- variable argument lists
.SH SYNOPSIS
.nf
.ft B
#include <stdarg.h>
void
va_start(va_list ap, last);
type
va_arg(va_list ap, type);
void
va_end(va_list ap);
.ft R
.fi
.SH DESCRIPTION
A function may be called with a varying number of arguments of varying
types.
The include file
.I <stdarg.h>
declares a type (\fIva_list\fP) and defines three macros for stepping
through a list of arguments whose number and types are not known to
the called function.
.PP
The called function must declare an object of type
.I va_list
which is used by the macros
.IR va_start ,
.IR va_arg ,
and
.IR va_end .
.PP
The
.I va_start
macro initializes
.I ap
for subsequent use by
.I va_arg
and
.IR va_end ,
and must be called first.
.PP
The parameter
.I last
is the name of the last parameter before the variable argument list,
i.e. the last parameter of which the calling function knows the type.
.PP
Because the address of this parameter is used in the
.I va_start
macro, it should not be declared as a register variable, or as a
function or array type.
.PP
The
.I va_start
macro returns no value.
.PP
The
.I va_arg
macro expands to an expression that has the type and value of the next
argument in the call.
The parameter
.I ap
is the
.I va_list ap
initialized by
.IR va_start .
Each call to
.I va_arg
modifies
.I ap
so that the next call returns the next argument.
The parameter
.I type
is a type name specified so that the type of a pointer to an
object that has the specified type can be obtained simply by
adding a
.I *
to
.IR type .
.PP
If there is no next argument, or if
.I type
is not compatible with the type of the actual next argument
(as promoted according to the default argument promotions),
random errors will occur.
.PP
The first use of the
.I va_arg
macro after that of the
.I va_start
macro returns the argument after
.IR last .
Successive invocations return the values of the remaining
arguments.
.PP
The
.I va_end
macro handles a normal return from the function whose variable argument
list was initialized by
.IR va_start .
.PP
The
.I va_end
macro returns no value.
.SH STANDARDS
The
.IR va_start ,
.IR va_arg ,
and
.I va_end
macros are ANSI C compatible.
.SH COMPATIBILITY
These macros are
.B not
compatible with the historic macros they replace.
A backward compatible version can be found in the include
file
.IR <varargs.h> .
.SH EXAMPLES
The function
.I foo
takes a string of format characters and prints out the argument
associated with each format character based on the type.
.sp
.nf
.RS
foo(fmt)
char *fmt;
{
va_list ap;
int d;
char c, *p, *s;
va_start(ap, fmt);
while (*fmt)
switch(*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\en", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\en", d);
break;
case 'c': /* char */
c = va_arg(ap, char);
printf("char %c\en", c);
break;
}
va_end(ap);
}
.fi