A question on C programming style

Paul D. Smith pds at lemming.webo.dg.com
Tue Apr 16 04:38:17 AEST 1991


I definitely prefer the nested form.  It does have disadvantages, but
they are far outweighed by the advantages (IMHO).  In my projects
we've had lots of libraries (code reusage :-), and the task of trying
to get every include file in the correct order was not worth the
effort.

[] On 13 Apr 91 00:11:42 GMT, amewalduck at trillium.waterloo.edu (Andrew Walduck) said:

AW> Disadvantages:
AW> 1. Slower compilation due to multiple references to the same file...

This is easily avoided by enhancing your multiple-inclusion scheme a
little bit: as above: in foo.h, use:

foo.h ------------------------------
/*
 * foo.h - prototypes for foo functions
 */

#ifndef FOO_H
#define FOO_H

...

#endif  /* FOO_H */
------------------------------ foo.h

Then, when including foo.h in another file, use:

#ifndef FOO_H
#include "foo.h"
#endif

This way the preprocessor doesn't even try to include the file if
FOO_H is already defined.  This is important if you have lots of
include files: I worked on a C compiler which ran out of space when we
had too many files included.

AW> 2. Must include references to the imbedded includes in the make file
AW>    dependancy list....

This is a pain, I admit.  You need a bit of trial-and-effort to get
the first file compiled: you find out you're missing a file & add its
directory to the compile line & retry, then you find out *that* file
needed another, so you add its directory and retry, etc.

However, you only have to do it for one or two files, then you've
generally gotten all of them.

AW> 3. Potential for loops if one file omits having a #ifndef, #endif wrapper
AW>    around it.

You probably won't get loops: most of the time you get redefinition
errors and your compile aborts.  This is usually trivial to find and
fix.

-----

Another popular scheme is to have one "master header file" which
includes every header file you need, then include the "master" in your
code, like this:

foo.h ------------------------------
/*
 * foo.h - Includes everything I need ...
 */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/time.h>
 ...
#include "bar.h"
 ...
------------------------------ foo.h

Then in your .c files you just say `#include "foo.h"' and nothing
else.

The disadvantage here, of course, is that the preprocessor has to
process all of these headers for each source file, even if most of it
isn't used.

I think the moral of the post is, as always, use the scheme which fits
your needs.  If you've got a large project with lots of
interdependencies, you should go with the nested-inclusion scheme to
save yourself headaches.  If you've got a simpler project, either go
with non-nested inclusion or use the "master header file" scheme.

                                                                paul
-----
 ------------------------------------------------------------------
| Paul D. Smith                          | pds at lemming.webo.dg.com |
| Data General Corp.                     |                         |
| Network Services Development Division  |   "Pretty Damn S..."    |
| Open Network Systems Development       |                         |
 ------------------------------------------------------------------



More information about the Comp.lang.c mailing list