Download C User's Guide

Transcript
To allow our example to compile with earlier compilers, we make extensive use of
the macro __STDC__ which is defined only for ISO C compilation systems. Thus, 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 */
}
Since the old-style variable argument mechanism did not allow us to specify any
fixed parameters, we must arrange for them to 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—the name of the parameter that comes
just before the “…” terminator.
As an extension, Sun ISO C allows functions to be declared and defined with no
fixed parameters, as in:
int f(...);
Chapter 6
Transitioning to ISO C
6-7