Converting ANSI-C to non-ANSI-C, Please help.

Guido van Rossum guido at cwi.nl
Mon Feb 11 23:11:30 AEST 1991


rfg at lupine.ncd.com (Ron Guilmette) writes:

>One thing that I have tried to stress to all users of protoize & unprotoize
>is that putting all of those #ifdef __STDC__ directives into your code is
>dumb, stupid, ugly, and unnecessary, and that it actually *degrades* (by
>a significant amount) the "quality" of your code.

Suppose I don't want to use unprotoize on the fly (don't ask me why,
maybe I'm just stubborn, maybe I develop part of my code that has
neither a Standard C compiler nor enough power to run unprotoize on
the fly (could it be a Mac?)).  What's the best thing to do?  I
think it's this; comments are welcome.  I use cpp to hide the
prototypes from the Classic C compilers as follows:

     #ifdef __STDC__
     #define PROTO(x) x
     #else
     #define PROTO(x) ()
     #endif

This is used like this in header files (you'll get used to the the
double parentheses very quickly).

     extern char *foo PROTO((int, double *, char **));

The Classic C compiler sees foo() while the Standard compiler sees
foo(int, double *, char **).

It is also usable in source files, provided you only use argument
types that don't need widening (so no chars, shorts or floats):

      char *foo PROTO((int, double *, char **));
      foo(size, result, list)
		int size;
		double *result;
		char **list;
      {
      	...
      }

Standard C compilers are required to accept this.  It is less ugly
than the solution with #ifdef __STDC__ at each function header, and
also keeps the two versions from diverging: every time you compile
with a Standard C compiler it will check correspondence between the
prototype and the classic header.

--
Guido van Rossum, CWI, Amsterdam <guido at cwi.nl>



More information about the Comp.lang.c mailing list