Go to main content
Oracle® Developer Studio 12.6: C User's Guide

Exit Print View

Updated: July 2017
 
 

7.2 Functions With Varying Arguments

In previous implementations, you could not specify the parameter types that a function expected, but ISO C encourages you to use prototypes to do just that. To support functions such as printf(), the syntax for prototypes includes a special ellipsis () terminator. Because an implementation might need to do unusual things to handle a varying number of arguments, ISO C requires that all declarations and the definition of such a function include the ellipsis terminator.

Because the “” part of the parameters have no name, a special set of macros contained in stdarg.h gives the function access to these arguments. Earlier versions of such functions had to use similar macros contained in varargs.h.

Assume that the function you want to write is an error handler called errmsg() that returns void, and whose only fixed parameter is an int that specifies details about the error message. This parameter can be followed by a file name, a line number, or both. These items are followed by format and arguments, similar to those of printf(), that specify the text of the error message.

For this example to compile with earlier compilers requires extensive use of the macro __STDC__, which is defined only for ISO C compilers. The function’s declaration in the appropriate header file is:

#ifdef __STDC__
    void errmsg(int code, ...);
#else
    void errmsg();
#endif

The file that contains the definition of errmsg() is where the old and new styles can get complex. First, the header to include depends on the compilation system:

#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <stdio.h>

stdio.h is included because we call fprintf() and vfprintf() later.

Next comes the definition for the function. The identifiers va_alist and va_dcl are part of the old-style varargs.h interface.

void
#ifdef __STDC__
errmsg(int code, ...)
#else
errmsg(va_alist) va_dcl /* Note: no semicolon! */
#endif
{
   /* more detail below */
}

Because the old-style variable argument mechanism did not allow you to specify any fixed parameters, they must be accessed before the varying portion. Also, due to the lack of a name for the “” part of the parameters, the new va_start() macro has a second argument, which is the name of the parameter that comes just before the “” terminator.

As an extension, Oracle Developer Studio ISO C allows functions to be declared and defined with no fixed parameters, as in:

int f(...);

For such functions, va_start() should be invoked with an empty second argument, for example:

va_start(ap,)

The following example is the body of the function:

{
    va_list ap;
    char *fmt;
#ifdef __STDC__
    va_start(ap, code);
#else
    int code;
    va_start(ap);
    /* extract the fixed argument */
    code = va_arg(ap, int);
#endif
    if (code & FILENAME)
        (void)fprintf(stderr, "\"%s\": ", va_arg(ap, char *));
    if (code & LINENUMBER)
        (void)fprintf(stderr, "%d: ", va_arg(ap, int));
    if (code & WARNING)
        (void)fputs("warning: ", stderr);
    fmt = va_arg(ap, char *);
    (void)vfprintf(stderr, fmt, ap);
    va_end(ap);
}

Both the va_arg() and va_end() macros work the same for the old-style and ISO C versions. Because va_arg() changes the value of ap, the call to vfprintf() cannot be:

(void)vfprintf(stderr, va_arg(ap, char *), ap);

The definitions for the macros FILENAME, LINENUMBER, and WARNING are presumably contained in the same header as the declaration of errmsg().

A sample call to errmsg() could be:

errmsg(FILENAME, "<command line>", "cannot open: %s\n",
argv[optind]);