moving towards the standard ANSI with old c code

Barry Margolin barmar at think.com
Sat Apr 20 08:06:13 AEST 1991


In article <8452 at umd5.umd.edu> jjk at astro.umd.edu (Jim Klavetter) writes:
>1.  What is the timescale for all compilers to be ANSI?  And what is
>the timescale for most compilers not accepting old c (I know it is
>often called K&R c, but I will use the term old C to mean nonANSI c)?

Conformance with standards is purely voluntary in the US, driven by market
forces.  Government contracts generally require products that conform to
national standards, so vendors who want to do business with the government
will probably have conforming compilers as soon as possible.  If compiler
purchasers stop giving business to vendors who supply nonstandard
compilers, they will become extinct pretty soon.  The biggest problem is
system vendors who include a C compiler with the OS -- purchasers of such
systems generally don't base their decision just on the C compiler that is
included, so they will often take a system with an old C compiler because
they want the features of the rest of the system.

>2.  When should I be worrying about updating my libraries (if not my
>programs) to ANSI c?  Should this be done all at once or can I do it
>as needed?

Your best bet is to start as soon as possible, but update it into code that
will work in both ANSI and pre-ANSI C.  You can use macros to minimize the
number of places where you must distinguish between the two dialects.  For
instance, you can put the following in a header file that all your code
includes:

#if _STDC_ == 1
#define ARGS(x) x
#else
#define ARGS(x)
#endif

Then you can write the following:

int foo (ARGS((char*, int)));

In ANSI C this will become a function prototype, and in pre-ANSI C it will
become a function declaration.

You can also try to find places in your code that are dependent on language
features that are different in the two dialects, such as the automatic
promotion of various arithmetic types during function calls, and recode
them so that they work in both environments.

Using techniques such as these, there's very little code that can't be made
to run in both dialects.  ANSI C was designed to be mostly upward
compatible with most common pre-ANSI implementations of C.

--
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list