ANSI/Non-ANSI Function Declaration Macros

Wayne Throop throopw at sheol.UUCP
Mon Dec 18 01:19:18 AEST 1989


> peter at isgtec.UUCP (Peter Curran)
> First, I assume that, in ANSI, the following are exactly equivalent:
>	void func (char c, short n) {...}
> and
>	void func (c, n) char c; short n; {...}
> They are just different syntax for the same thing.  I would like to be
> corrected if I have misunderstood this.

I am pretty sure that Peter has indeed misunderstood this.
A prototype declaration must be matched with a prototype definition.

I no longer have a copy of the standard directly to hand, but K&RII have
some things to say about it that make me quite sure.

> My prototype here would be    void func P((char c, short n));

In order to make the definitions and declarations agree, I use a more
complicated scheme involving the three macros PT, PF, and PP, standing
for "prototype", "protype function", and "prototype punctuation".  This
has been known to make people barf, though compilers seem to like it
just fine.  So, beware when reading on:

    return_type func PT((type1 arg1, type2 arg2, type3 arg3));

    ...

    return_type func PF((arg1, arg2, arg3),
                         type1 arg1 PP
                         type2 arg2 PP
                         type3 arg3 )
    { ... }

Now, P, PF and PP have the obvious definitions, which I'll give below.
They can be put in "protomac.h" and largely forgotten about.

This scheme does have the problem that you MUST have the arguments in
the correct order, and don't change your mind between the first
(typeless) and second (typed) list of arguments.  This is not horribly
more odious than bare K&R style declarations, so I live with it, and
await the day when K&R-only implementations are rare enough to dispense
with the charade. 

protomac.h:

    #ifndef PF
    #ifdef __STDC__
    #define PF(names,types) (types)
    #define PT(types) types
    #define PP ,
    #else
    #define PF(names,types) names types;
    #define PT(types) ()
    #define PP ;
    #endif
    #endif
--
Wayne Throop <backbone>!mcnc!rti!sheol!throopw or sheol!throopw at rti.rti.org



More information about the Comp.lang.c mailing list