ANSI/Non-ANIS Function Declaration Macros

Geoff Clare gwc at root.co.uk
Tue Dec 12 23:30:07 AEST 1989


In article <4603 at itivax.iti.org> scs at itivax.iti.org (Steve Simmons) writes:
>
>Currently I'm writing code that must compile with and without ANSI
>C features.  Ideally this could be done with no slgging the code
>with lots of #ifdef constructs like
>
>#ifdef __STDC__
>int	*foo( int const a )
>#else
>int	*foo( a )
>int	a ;
>#endif
>{
>	func body
>}
>
>While this works, it's got a weakness.  In having two separate declarations,
>one can over time let them get out of sync.  It's also a pain to write.
>Has anybody come up with a way of doing this so that I write the declarations
>once and once only?  Bizarre constructs considered within limits.

My recommendation is that you do the following:

	#ifndef __STDC__
	#define const
	#endif

	int *foo(a)
	const int a;

Old style declarations are accepted equally well by ANSI compilers
as 'common C' ones.  The only thing that needs to be dependent on
__STDC__ being defined is the 'const' keyword.

"But what do I do about prototypes?" I hear you ask.

My answer is the same: just stick to old-style 'extern' declarations.
The advantage of using prototypes is that you don't need to worry
about casting function arguments correctly all the time.  But if
your code must compile with non-ANSI compilers, you're going to have
to cast all the arguments anyway - so the prototypes are redundant.

-- 
Geoff Clare, UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc at root.co.uk  (Dumb mailers: ...!uunet!root.co.uk!gwc)  Tel: +44-1-315-6600



More information about the Comp.lang.c mailing list