This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.

Released under the GNU General Public License.

Return to index Return to header Previous symbol Next symbol

va_list

Prototype

#include <stdarg.h>

typedef implementation-defined va_list;

Description

The va_list type is used to access the variable arguments of a function. It is required to scan all the arguments using the va_arg macro and should be initialized with va_start and cleaned up with va_end before returning from the function.

Example

View source
#include <stdarg.h>
#include <stdio.h>

/*
 * A mini vfprintf() function.
 *
 * Accepts the following printf() formats:
 *   %%  prints %
 *   %c  prints a char
 *   %d  prints an int
 *   %s  prints a string (char *)
 */
void mini_vfprintf(FILE *stream, const char *fmt, va_list ap)
{
    for (; *fmt != '\0'; ++fmt)
	if (*fmt == '%')
	    switch (*++fmt) {
	    case '%':
		fputc('%', stream);
		break;
	    case 'c':
		fprintf(stream, "%c", va_arg(ap, char));
		break;
	    case 'd':
		fprintf(stream, "%d", va_arg(ap, int));
		break;
	    case 's':
		fprintf(stream, "%s", va_arg(ap, char *));
		break;
	    }
	else
	    fputc(*fmt, stream);
}

/*
 * A mini fprintf() function.
 */
void mini_fprintf(FILE *stream, const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    mini_vfprintf(stream, fmt, ap);
    va_end(ap);
}

/*
 * A mini printf() function.
 */
void mini_printf(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    mini_vfprintf(stdout, fmt, ap);
    va_end(ap);
}

References

ISO C 9899:1990 7.8