Index of Section 3 Manual Pages
| Interix / SUA | va_end.3 | Interix / SUA |
va_end(3) va_end(3)
varargs()
NAME
varargs - variable argument lists
SYNOPSIS
#include
type va_arg (argp, type)
va_list argp;
void va_start (argp, paramn)
va_list argp;
void va_end (argp)
va_list argp;
or
#include
va_alist argp;
va_dcl
void va_start (argp)
va_list argp;
type va_arg (argp, type)
va_list argp;
void va_end (argp)
va_list argp;
DESCRIPTION
You use the varargs(3) set of macros to write subroutines that accept a
variable number of arguments in a portable fashion. Unless it uses the
varargs macros, a subroutine that has a variable-length parameter list
cannot be ported unchanged to other operating systems because they use
different conventions for passing arguments.
The argp parameter specifies a variable that the varargs macros use to
track the current location in the parameter list. This variable is read-
only.
The type argument specifies the data type that the expected argument will
be converted to when it is passed as an argument. Arguments that are char
or short should be accessed as int. Arguments that are unsigned char or
short are converted to unsigned int. Arguments that are float are
converted to double. When types are mixed, the subroutine is responsible
for determining the expected type because the type cannot be determined at
run time.
The parmn argument identifies the rightmost parameter in the function
definition.
The definitions provided in varargs.h are deprecated and are provide
mainly for backward compatibility. For ANSI-standard code, use stdarg(3)
instead.
Declarations in stdarg.h
va_start
This macro initializes the argp parameter so that it points to the
beginning of the argument list. The parmn parameter identifies the
rightmost argument in the function definition. By default, it
identifies the first argument in the argument list, which can be a
pointer, an integer, or a double. The va_start macro is started before
any unnamed arguments are accessed.
Declarations in varargs.h
va_start
Initializes the argp parameter so that it points to the beginning of
the argument list.
va_alist
Used as the parameter list in the function header.
va_argp
Used by the varargs macro to track the current location in the
argument list. This variable is read-only.
va_dcl
The declaration for va_list; as such, it should not be followed by a
semicolon (;).
Declarations in both stdargs.h and varargs.h
va_list
Defines the type of the variable used to traverse the list.
va_arg
Expands to an expression that has the type and value of the next
argument in the function call. The argp argument is the va_list argp
initialized by va_start. Each call to va_arg modifies argp so that the
type of a pointer to an object that has the specified type can be
obtained by simply adding a * to type.
If there is no next argument, or if type is not compatible with the
type of the actual next argument (as promoted according to the default
argument promotions), random errors will occur.
The first use of the va_arg macro after va_start returns the argument
after parmn. Successive invocations return the values of the remaining
arguments.
va_end
Handles a normal return from the function whose variable argument list
was initialized by va_start. It has no return value.
EXAMPLE
The function Myprintf takes a string of format characters and prints out
the argument associated with each format character based on the type. The
arguments some_i fmt indicate only that the function should take at least
one actual argument.
#include
#if __STDC__
#include
#else
#include
#endif
#if __STDC__
Myprintf(int some_i , const char *fmt, ...)
#else
Myprintf(some_i, fmt, va_alist)
int some_i;
char *fmt;
va_dcl
#endif
{
int ret;
int i ;
float f;
char c ;
va_list ap;
#if __STDC__
va_start(ap, fmt);
#else
va_start(ap);
#endif
i = (int) va_arg(ap,int);
f = (float) va_arg(ap,double);
c = (char) va_arg(ap,int);
printf("%d, %f, %c\n",i,f,c);
va_end(ap);
return (ret);
}
int main(){
int i = 10, some_i = 0 ;
float f = 100 ;
char c = 'c';
Myprintf(some_i,"Some_String",i,f,c);
}
USAGE NOTES
None of these functions are thread safe.
None of these functions are async-signal safe.