| Header File |
Article Index for Header |
Website Links For File |
Information AboutHeader File |
| CATEGORIES ABOUT HEADER FILE | |
| source code | |
| c programming language | |
| c | |
|
A header file commonly contains Forward Declaration s of Subroutine s, Variable s, and other Identifier s. Identifiers that need to be declared in more than one source file can be placed in one header file, which is then included whenever its contents are required. In the C and C++ programming languages, Standard Library functions are traditionally declared in header files; see C Standard Library and C++ Standard Library for examples. MOTIVATION In most modern computer programming languages, Program s can be broken up into smaller components such as Subroutine s, and those components can be distributed among many physical source File s, which are Compiled Separately . Once a subroutine needs to be used somewhere other than where it's defined, the concept of Forward Declaration s or Function Prototype s must be introduced. For example, a function defined in this way in one source file: may be declared (with a Function Prototype ) and then referred to in a second source file, thus: However, this simplistic approach requires that the programmer maintain the function declaration for add in two places — in the file containing its implementation and in the file where it's used. If the definition of the function ever changes, the programmer must remember to update all the prototypes scattered across the program, as well.Header files provide the solution. A module's header file declares each function, object, and data type that is part of the ''public interface'' of the module — for example, in this case the header file would include only the declaration of add. Each source file that refers to add uses the #include directive to bring in the header file:This reduces the maintenance burden: when a definition is changed, only a single copy of the declaration must be updated (the one in the header file). The header file may also be included in the source file that contains the corresponding definitions, giving the compiler an opportunity to check the declaration and the definition for consistency. Typically, header files are used to specify only Interfaces , and usually provide at least a small amount of documentation explaining how to use the components declared in the file. As in this example, the implementations of subroutines are left in a separate source file, which continues to be compiled separately. (One common exception in C and C++ is Inline Function s, which are often included in header files because most implementations cannot properly expand inline functions without seeing their definitions at Compile Time .) ALTERNATIVES Header files are not the only solution to the problem of accessing identifiers declared in different files. They have the disadvantage that it may still be necessary to make changes in two places (a source file and a header file) whenever a definition changes. Some newer languages (such as Java ) dispense with header files and instead use a Naming Scheme that allows the compiler to locate the source files associated with interfaces and class implementations. SEE ALSO EXTERNAL LINKS
|
|
|