Function declarations (was: MSC v5.1 Bug???)

T. William Wells bill at proxftl.UUCP
Fri Aug 26 11:06:03 AEST 1988


A random point on prototypes: we have the need to include
prototypes in our code while still being able to compile the code
with compilers that did not support prototypes.

The solution is this: first, all function arguments are of a type
for which the argument widening action is null.  (ints are OK,
short is not.) Second, we have, in a global include file,
something like this:

	/* Configuration section */

	#define ANSI_PROTO      /* This line would be added if the
				   compiler has prototypes.  Note that
				   just using __STDC__ is not sufficient,
				   as there are many compilers that have
				   prototypes but do not define it.  */

	/* This stuff is buried somewhere deep in the include file, the
	   person compiling our code does not look at it. */

	#ifdef ANSI_PROTO
	#define FUNC(n,p) n p;
	#else
	#define FUNC(n,p) n ();
	#endif

This is the code that makes use of the prototype macro.

	#include <our_file.h>

	FUNC(int function, (int arg1, int arg2))

What happens is that, in the default file we ship, ANSI_PROTO is
not specified and the FUNC macro will expand into something like

	extern int function();

And, since we use the right argument types, this will work on
non-ANSI or ANSI compilers.  If, however, the user *does* define
ANSI_PROTO, the FUNC macro expands to

	extern int function(int arg1, int arg2));

and he gets the type checking benefits of the prototype.


---
Bill
novavax!proxftl!bill



More information about the Comp.lang.c mailing list