A question on C programming style

Richard Barnette barnettr at snaggle.rtp.dg.com
Sun Apr 14 06:49:16 AEST 1991


In article <1991Apr12.103621.8907 at umiami.ir.miami.edu> devebw9f at miavax.ir.miami.edu writes:
>How do the guru's on the net feel about the following two styles?
>
>Style 1: (No nested includes - user responsible for proper order of includes).
    I tend not to prefer this style.  Forcing the user of header files
to be aware of other header dependencies drastically complicates the
programming process.
    Things get further complicated when the dependencies change
(and they almost always do).  Then some poor joe (probably not the
one who wrote the original code) has to find all the places where the
header was included, and add more #include's to handle the new
dependency.  Or else you forget to delete extra #include's when
a dependency is dropped.
    There are some advantages with this style (see below).

>Style 2: (Nested inclusion).
    My preference.  Note however that care should be taken not
to include a header twice.  This can produce warnings and/or errors
depending on the kinds of things in the header.  It's wise to make
all your #include'd headers look something like this:

#ifndef foo
#define foo
/* contents of file "foo.h" */
#endif

    Unfortunately, some systems don't do something like this
with their standard header files.  A #include <stdio.h> inside
your header file may look innocuous enough, but if the header
is included by a file with its own #include <stdio.h>, disaster
may strike.

Richard Barnette      | Data General Corporation | obligatory (in)famous quote:
(919) 248-6225        | RTP, NC  27709	         |  You wascal wabbit!
Commercial Languages  | 62 T.W. Alexander Drive	 |  Wandering wizards won't
barnettr at dg-rtp.dg.com				 |  win! - /usr/lib/sendmail



More information about the Comp.lang.c mailing list