COHERENT manpages

This page displays the COHERENT manpage for stdarg.h [Header for variable numbers of arguments].

List of available manpages
Index


stdarg.h -- Header File

Header for variable numbers of arguments
#include <stdarg.h>

stdarg.h is  the header  file that  ANSI C uses  to declare and  define the
routines that  traverse a variable-length  argument list.  It  declares the
type va_list and defines the macros va_arg(), va_start(), and va_end().

Example

The following example concatenates multiple strings into a common allocated
string  and returns  the  string's address.   method  of handling  variable
arguments:

#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>

char *
multcat(numargs)
int numargs;
{
    va_list argptr;
    char *result;
    int i, siz;

    /* get size required */
    va_start(argptr, numargs);
    for(siz = i = 0; i < numargs; i++)
        siz += strlen(va_arg(argptr, char *));

    if ((result = calloc(siz + 1, 1)) == NULL) {
        fprintf(stderr, "Out of space\n");
        exit(EXIT_FAILURE);
    }
    va_end(argptr);

    va_start(argptr, numargs);
    for(i = 0; i < numargs; i++)
        strcat(result, va_arg(argptr, char *));
    va_end(argptr);
    return(result);
}

int
main()
{
    printf(multcat(5, "One ", "two ", "three ",
        "testing", ".\n"));
}

See Also

header files,
varargs.h
ANSI Standard, §7.8

Notes

The routines defined  in <stdarg.h> were first implemented under UNIX
System V, where they are declared in the header file <varargs.h>. The
ANSI  C  committee  recognized  the  usefulness of  <varargs.h>,  but
decided that  it had  semantic problems.  In  particular, <varargs.h>
introduced  the  notion of  declaring  ``...''  for the  variable-arguments
argument list  in the function  prototype.  This, unfortunately,  left them
with declarations of the form

    void error(...)
    {
        whatever
    }

and no obvious hook for accessing the parameter list within the body of the
function.   So,  the  ANSI committee  changed  the  header declaration:  it
insisted  on  one  or more  formal  parameters,  followed  by  the list  of
variables.

The committee had  the wisdom to change the name  of its header file, hence
<stdarg.h> came  into being.   Unfortunately, the committee  kept the
same  macro names,  but in  one  macro (va_start())  changed the  number of
arguments it takes.

COHERENT includes  both <varargs.h> and  <stdarg.h>, to support
both ANSI and System-V code.