<varargs.h> vs. function prototypes

Mark Hall markhall at pyrps5.pyramid.com
Sat Jun 15 03:51:43 AEST 1991


In article <25353 at oolong.la.locus.com> scotte at locus.com (Scott D. Eberline) writes:
>For software that may potentially be ported to platforms not yet
>ANSI-compliant, some functions taking variable arguments are
>defined using <varargs.h> instead of <stdarg.h>.  I would like
>to declare full prototypes for these to avoid compiler warnings
>about missing prototypes.  In the module in which these functions
>are defined, this doesn't seem to be possible:

OK, so you want your code to work with ANSI and non-ansi compilers.
Why not accomodate both in your source.  That's what the predefined
__STDC__ macro is for.

Declarations:
	#ifdef __STDC__
	void foo(char *format, ...);
	#else
	void foo();
	#endif

Definitions:

	#ifdef __STDC__
	void foo(char *format, ...)
	#else
	void foo(format, va_alist) char *format; va_dcl
	#endif
	{
		va_list ap;
		#ifdef __STDC__
			va_start(ap,format);
		#else
			va_start(ap);
		#endif
		
		[ ... rest of stuff the same ... ]
	}

>I'm a bit resigned to having missing prototype warnings for the module
>that contains the function definitions.  I'd still like to have
>prototypes in all the other modules, and the ellipsis notation
>seems to be the only way to do this.

It's nice to have a compiler which requires having prototypes if you
want them.  Of course, this isn't required by ANSI.

>How dangerous is it to call functions declared externally using the
>ellipsis notation, but defined using va_dcl?  

Very.  If a stdarg prototype is visible at a call site, it allows the
compiler to generate a different calling sequence.  For example, if a
compiler normally passes arguments in registers, it may choose to just
load them on the data stack instead when it sees a stdarg call site
(an optimization; otherwise it would just have to store down the 
registers on function entry).  Since it can depend on the definition of
the function to also have the stdarg notation, it will know how to
"pick up" the arguements in the function body.

>May a conforming implement-
>ation assume that functions with ellipsis prototypes use <stdarg.h>-style
>arguments and not <varargs.h>-style?

Well, it may assume that an ellipsis prototype was visible at every call
site of "foo" if "foo" is defined using the ellipsis notation.

>-- 
>Scott D. Eberline			scotte at locus.com  or  lcc!scotte

-Mark Hall (smart mailer): markhall at pyrps5.pyramid.com
	   (uucp paths): {uunet|ames|decwrl|sun|seismo}!pyramid!markhall



More information about the Comp.std.c mailing list