Header Files

Header Files

An operating system project usually includes some number of directories, each containing many .c files containing the code for some part of the system, along with some .h header files that contain declarations and definitions used by one or more code files. Header files can also contain simple macros, such as

#define BUFFER_SIZE 4096

which allows the programmer to name constants, so that when BUFFER_SIZE is used in the code, it is replaced during compilation by the number 4096. Good C programming practice is to name every constant except 0, 1, and -1, and sometimes even them. Macros can have parameters, such as

#define max( a, b) (a > b ? a : b)

which allows the programmer to write

i = max(j, k+1)

and get

i = (j > k+1 ? j : k+1 )

to store the larger of j and k+1 in i. Headers can also include conditional compilation, for instance

#ifdef PENTIUM
#end if

which compiles into a call to the function intel_int_ack if the macro PENTIUM is defined and nothing otherwise. Conditional compilation is heavily used to isolate architecture-dependent code so that certain code is inserted only when the system is compiled on the Pentium, other code is inserted only when the system is compiled on a SPARC, and so on. A .c file can bodily contain zero or more header files using the #include directive. There are also many header files that are common to nearly every .c and are stored in a central directory.


operating system, header files, macros