If we adopt a modular approach then we will naturally want to keep variable definitions, function prototypes etc. with each module. However what if several modules need to share such definitions?
It is best to centralise the definitions in one file and share this file amongst the modules. Such a file is usually called a header file.
Convention states that these files have a .h suffix.
We have met standard library header files already e.g:
We can define our own header files and include then our programs via:
NOTE: Header files usually ONLY contain definitions of data types, function prototypes and C preprocessor commands.
Consider the following simple example of a large program (Fig. ) - see Appendix main.c, WriteMyString.c and header.h for full listings.
We would usually compile each module separately (more later).
Some modules have a #include ``header.h'' that share common definitions.
Some, like main.c, also include standard header files also.
main calls the function WriteMyString() which is in WriteMyString.c module.
The function prototype void for WriteMyString is defined in Header.h
NOTE that in general we must resolve a tradeoff between having a desire for each .c module to have access to the information it needs solely for its job and the practical reality of maintaining lots of header files.
Up to some moderate program size it is probably best to one or two header files that share more than one modules definitions.
For larger programs get UNIX to help you (see later).
One problem left with module approach:
If we have global variables declared and instantiated in one module how can pass knowledge of this to other modules.
We could pass values as parameters to functions, BUT: