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_start

Prototype

#include <stdarg.h>

void va_start(va_list ap, paramN);

Description

The va_start macro initializes the variable arguments data used for scanning the list of arguments. The paramN parameter is the last parameter in the function argument list; for example in the following function:
void f(void *p, int i, ...);
where the i variable is the last argument, the va_start function should be called as:
va_list ap;
va_start(ap, i);

Example

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

/*
 * Print the arguments plus their type.
 * The first argument `fmt' specifies the type of the other
 * arguments:
 *   c means char
 *   d means int
 *   s means string (char *)
 * For example:
 *    print_types("cds", 'x', 10, "abc");
 * Prints:
 *    char = 'x'
 *    int = 10
 *    string = "abc"
 */
void print_types(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    for (; *fmt != '\0'; ++fmt)
	switch (*fmt) {
	case 'c':
	    printf("char = '%c'\n", va_arg(ap, char));
	    break;
	case 'd':
	    printf("int = %d\n", va_arg(ap, int));
	    break;
	case 's':
	    printf("string = \"%s\"\n", va_arg(ap, char *));
	    break;
	}
    va_end(ap);
}

References

ISO C 9899:1990 7.8.1.1