Programming in C

(Barry) #1
Communication Between Modules 341

Using Header Files Effectively


In Chapter 13, “The Preprocessor,” you were introduced to the concept of the include
file. As stated there, you can group all your commonly used definitions inside such a file
and then simply include the file in any program that needs to use those definitions.
Nowhere is the usefulness of the #includefacility greater than in developing programs
that have been divided into separate program modules.
If more than one programmer is working on developing a particular program, include
files provide a means of standardization: Each programmer is using the same definitions,
which have the same values. Furthermore, each programmer is thus spared the time-
consuming and error-prone task of typing these definitions into each file that must use
them.These last two points are made even stronger when you start placing common
structure definitions, external variable declarations,typedefdefinitions, and function
prototype declarations into include files.Various modules of a large programming system
invariably deal with common data structures. By centralizing the definition of these data
structures into one or more include files, you eliminate the error that is caused by two
modules that use different definitions for the same data structure. Furthermore, if a
change has to be made to the definition of a particular data structure, it can be done in
one place only—inside the include file.
Recall your datestructure from Chapter 9, “Working with Structures”; following is
an include file that might be similar to one you would set up if you have to work with a
lot of dates within different modules. It is also a good example of how to tie together
many of the concepts you’ve learned up to this point.


// Header file for working with dates


#include <stdbool.h>


// Enumerated types


enum kMonth { January=1, February, March, April, May, June,
July, August, September, October, November, December };


enum kDay { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };


struct date
{
enum kMonth month;
enum kDay day;
int year;
};

Free download pdf