Index Home About Blog
From: (Chris Torek)
Newsgroups: comp.lang.c
Subject: Re: What is this???
Date: 4 Aug 1998 20:59:19 -0700

In an article my server has not received, James Hu wrote:
>>The only things that made the code non-conforming were the lack of
>>"..." and the missing call to va_end().  Putting the "..." into the
>>function made it necessary to use the improved ANSI C function
>>declaration syntax.

In article <> Cyrand <> wrote:
>Thanks.  Can I mix declaration syntaxes?

Yes.  You can also mix prototype declarations with old-style
definitions, e.g.,

	int	f1(int, char *);
	int	f2(void);

	f1(i, p)
		int i;
		char *p;
		/* ... */
		return 17;

		/* ... */
		return 42;

You must, however, be aware of several restrictions.  First, all
variable-arguments functions must be prototyped, and their definitions
must use the newfangled 1989 prototype definition format (as James
Hu mentioned).  Second, old-style function definitions do not provide
prototypes and need not be verified against the prototype, so that,
for instance:

	int f3(int);
	int f3(p) char *p; { return *p; }

need not draw a diagnostic (despite being obviously broken).  Third
and probably most tricky, you must widen all the arguments according
to "the usual arithmetic conversions": char and short become int,
and float becomes double. This means that the correct prototype
for a function *defined* as:

	f4(c, x)
		char c;
		float x;
		/* ... */


	void f4(int, double);

and *not* the obvious but wrong "void f4(char, float)".

The fact that you can mix these means that you can do the following:

	#define PROTO(args) args
	#define PROTO(args) ()

	int f1 PROTO((int, char *));
	int f2 PROTO((void));
	int f3 PROTO((int));
	int f4 PROTO((int, double));

This kind of PROTO macro (sometimes spelled just "P", or in
implementation-specific headers like those in 4.4BSD's /usr/include
directory, __P) allows you to get the benefits of prototypes while
still being able to compile on creaky old pre-1990 systems.
In-Real-Life: Chris Torek, Berkeley Software Design Inc
El Cerrito, CA	Domain:	+1 510 234 3167
Antispam notice: unsolicited commercial email will be handled at my
consulting rate; pyramid-scheme mail will be forwarded to the FTC.

Index Home About Blog