stdarg(5) stdarg(5)
NAME [Toc] [Back]
stdarg.h - macros for handling variable argument lists
SYNOPSIS [Toc] [Back]
#include <stdarg.h>
void va_start(va_list pvar, argN);
type va_arg(va_list pvar, type);
void va_end(va_list pvar);
DESCRIPTION [Toc] [Back]
The <stdarg.h> header contains a set of macros that can be used to
write portable procedures that accept variable argument lists.
Routines that have variable argument lists (such as printf()) but do
not use stdarg are inherently nonportable, because different machines
use different argument-passing conventions.
va_list is a type defined for the variable used to traverse the list.
va_start is called to initialize pvar to the beginning of the list.
The type of argN should be the same as the argument to the function
just before the variable portion of the argument list.
va_arg returns the next argument in the list pointed to by pvar. type
is the type the argument is expected to be. Different types can be
mixed, but it is up to the routine to know what type of argument is
expected, because it cannot be determined at runtime.
va_end is used to clean up.
Multiple traversals, each bracketed by va_start ... va_end, are
possible.
NOTE: The <stdarg.h> header file supercedes the <varargs.h> header,
and contains all of the varargs macros. <varargs.h> is provided for
compatibility with pre-ANSI compilers and earlier releases of HP
C/HP-UX.
EXAMPLE [Toc] [Back]
This example is a possible implementation of execl (see exec(2)):
#include <stdarg.h>
#define MAXARGS 100
/* execl is called by
execl(file, arg1, arg2, ..., (char *)0);
*/
execl(const char *file, const char *args, ...)
{
va_list ap;
char *array[MAXARGS];
Hewlett-Packard Company - 1 - HP-UX 11i Version 2: August 2003
stdarg(5) stdarg(5)
int argno = 0;
va_start(ap, args);
if ((array[0] = args) != 0)
while ((array[argno++] = va_arg(ap, char *)) != 0)
;
va_end(ap);
return execv(file, array);
}
WARNINGS [Toc] [Back]
It is up to the calling routine to specify how many arguments there
are, since it is not always possible to determine this from the stack
frame. For example, execl() is passed a zero pointer to signal the
end of the list, and printf() can tell how many arguments are there by
the format string.
Unless ANSI C is used, it is non-portable to specify a second argument
of char, short, or float to va_arg, because arguments seen by the
called function are never char, short, or float.
Pre-ANSI C converts char and short arguments to int and converts float
arguments to double before passing them to a function.
SEE ALSO [Toc] [Back]
exec(2), vprintf(3S), varargs(5).
STANDARDS CONFORMANCE [Toc] [Back]
<stdarg.h>: AES, SVID3, XPG4, FIPS 151-2, POSIX.1, ANSI C
va_arg: SVID3, XPG4, ANSI C
va_end: SVID3, XPG4, ANSI C
va_list: SVID3, XPG4, ANSI C
va_start: SVID3, XPG4, ANSI C
Hewlett-Packard Company - 2 - HP-UX 11i Version 2: August 2003 [ Back ] |