25.1 Declaring ``varargs'' Functions

The ANSI/ISO C Standard requires that all functions which accept a variable number of arguments be declared explicitly to do so, and also that a function prototype be ``in scope'' (that is, available) whenever a varargs function is called. You may remember that, under certain circumstances, unknown functions can be called at will, with the compiler assuming that they take ``normal'' arguments and return int. However, that exception does not apply here. (In other words, for varargs functions, function prototypes aren't just a good idea, they're the law.) The reason that prototypes are strictly required is that varargs functions may use special calling sequences; that is, the compiler may have to generate special code for these calls.

The presence of a variable-length argument list is indicated by an ellipsis in the prototype. For example, the prototype for printf, as found in <stdio.h>, looks something like this:

	extern int printf(const char *, ...);
Those three dots ... don't mean that I left something out; they are the ellipsis notation; this is the syntax that C uses to indicate the presence of a variable-length argument list. This prototype says that printf's first argument is of type const char *, and that it takes a variable (and hence unspecified) number of additional arguments.

The ellipsis notation must follow the ordinary, fixed arguments, and there must be at least one fixed argument. It is impossible in Standard C to define a function which accepts only variable arguments. (In general, this is not too much of a restriction, because the function must always be able to determine, for itself, how many arguments there are in the list, invariably by inspecting the first argument(s) in the list. In other words, the function will generally need at least one well-defined, fixed argument anyway, to get a toehold on the problem of figuring out what the other arguments are.)

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback