V

Top  Previous  Next

va_arg

#include <stdarg.h>

type va_arg (

   va_list ap,

   type

);

stand-alone

The va_arg macro is used to access the next argument in a variable-length argument list. The parameter ap should be a variable of the type va_list, which is defined in the stdarg.h header; it must have been initialized by va_start. The macro expands into an expression that has the value of the next argument and the specified type; if this is not in fact the type of the argument, or if there are no more arguments, the behaviour is undefined.

 

For example:

 

#include <stdarg.h>

void ourfunc(char *message, ...) {

   va_list ap;

   int ival;

   va_start(ap, message);

   ival = va_arg(ap, int);

   va_end(ap);

   ...

}

 

On each call of va_arg, the parameter ap is modified to point to the next argument in the list.

va_end

#include <stdarg.h>

void va_end (

   va_list ap

);

stand-alone

When accessing a variable length argument list, a function should call this macro once all the arguments have been processed. This ensures a correct return to the calling function. The parameter ap should be a variable of the type va_list, which is defined in the stdarg.h header; it must have been initialized by va_start.

va_start

#include <stdarg.h>

void va_start (

   va_list ap,

           parmN

);

stand-alone

va_start is called before accessing a variable-length argument list. The parameter ap should be a variable of the type va_list, which is defined in the stdarg.h header. The parameter parmN should be the parameter in the variable-argument list immediately before the '...'.

vfprintf

#include <stdio.h>

int vfprintf (

   FILE    *stream,

   char    *format,

   va_list ap

);

server

This function corresponds to fprintf, and performs formatted output to the specified stream. As with fprintf, the format argument controls the conversions to be performed. However, the variable argument list has been replaced by the single argument ap, which should be an argument pointer initialized by va_start.

 

For example:

 

#include <stdarg.h>

#include <stdio.h>

void error(char *func_name, char *format, ...) {

va_list ap;

va_start(ap, format);

fprintf(stderr, "Error in %s: ", func_name);

vfprintf(stderr, format, ap);

va_end(ap);

}

 

The function returns the number of characters output, or a negative value if an output error occurred.

vprintf

#include <stdio.h>

int vprintf (

   char    *format,

   va_list  ap

);

server

This function corresponds to printf, and performs formatted output to the standard output stream, stdout. As with printf, the format argument controls the conversions to be performed. However, as with vfprintf, the variable argument list has been replaced by the single argument ap, which should be an argument pointer initialized by va_start.

 

The function returns the number of characters output, or a negative value if an output error occurred.

vsnprintf

#include <stdio.h>

int vsnprintf (

   char       *pstr,

   size_t      size,

   const char *format,

   va_list     ap

);

server

vsnprintf corresponds to snprintf, and writes formatted output into a character array via a pointer pstr supplied by you. As with snprintf, the format argument controls the conversions to be performed. However, as with vfprintf, the variable argument list has been replaced by the single argument ap, which should be an argument pointer initialized by va_start. The number of characters written will not exceed the value of the size argument; extra characters are discarded. The meaning of the format string and the use of the other arguments is as for fprintf.

 

A NUL character automatically terminates the output string written to pstr unless size is zero, in which case nothing is written and pstr may be NULL. Note that this terminator is not included in the character count returned by vsnprintf.

 

The function normally returns the number of characters (octets) actually written into the array, but if size is zero, it returns the number of characters that an equivalent call to fprintf would have output.

vsprintf

#include <stdio.h>

int vsprintf (

   char    *pstr,

   char    *format,

   va_list  ap);

server

This function corresponds to sprintf, and writes formatted output into a character array via a pointer pstr supplied by you. As with sprintf, the format argument controls the conversions to be performed. However, as with vfprintf, the variable argument list has been replaced by the single argument ap, which should be an argument pointer initialized by va_start.

 

The function returns the number of characters output, or a negative value if an output error occurred.