All Topics  
Header file

 

   Email Print
   Bookmark   Link






 

Header file



 
 
In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, particularly in the C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 and C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 programming languages, a header file or include file is a file
Computer file

A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable computer storage....
, usually in the form of source code
Source code

In computer science, source code is any collection of statements or declarations written in some human-readable computer programming language....
, that a compiler
Compiler

A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
 automatically includes when processing another source file. Typically, programmers specify the inclusion of header files via compiler directives at the beginning (or head) of the other source file.

A header file commonly contains forward declarations of class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
es, subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s, variables, and other identifier
Identifier

In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems....
s.






Discussion
Ask a question about 'Header file'
Start a new discussion about 'Header file'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer programming
Computer programming

Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language....
, particularly in the C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 and C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 programming languages, a header file or include file is a file
Computer file

A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable computer storage....
, usually in the form of source code
Source code

In computer science, source code is any collection of statements or declarations written in some human-readable computer programming language....
, that a compiler
Compiler

A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
 automatically includes when processing another source file. Typically, programmers specify the inclusion of header files via compiler directives at the beginning (or head) of the other source file.

A header file commonly contains forward declarations of class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
es, subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s, variables, and other identifier
Identifier

In computer science, Identifiers are Lexical Token s that name entity. The concept is analogy to that of a "name". Identifiers are used extensively in virtually all information processing systems....
s. Programmers who wish to declare standardized identifiers in more than one source file can place such identifiers in a single header file, which other code can then include whenever the header contents are required.

The C standard library
C standard library

The C standard library consists of a set of sections of the ISO C standard which describe a collection of header files and library routines used to implement common operations, such as input/output and character string handling, in the C ....
 and C++ standard library
C++ standard library

In C++, the Standard Library is a collection of class and subroutine, which are written in the core language. The Standard Library provides several generic containers, functions to utilise and manipulate these containers, function objects, generic strings and streams , support for some language features, and every day functions for tasks suc...
 traditionally declare their standard functions in header files.

Motivation

In most modern computer programming languages, programmers can break up programs
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 into smaller components (such as class
Class

Class may refer to:...
es and subroutine
Subroutine

In computer science, a subroutine or subprogram is a portion of computer code within a larger computer program, which performs a specific task and is relatively independent of the remaining code....
s) and distribute those components among many translation unit
Translation unit

In the field of translation, a translation unit is a segment of a text which the translator treats as a single cognitive unit for the purposes of establishing an equivalence....
s (typically in the form of physical source file
Computer file

A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable computer storage....
s), which the system can compile
Compiler

A compiler is a computer program that transforms source code written in a programming language into another computer language . The most common reason for wanting to transform source code is to create an executable program....
 separately. Once a subroutine needs to be used somewhere other than in the translation unit where it is defined, the concept of forward declarations or function prototype
Function prototype

A function prototype in C or C++ is a declaration of a subroutine that omits the function body but does specify the function's name, arity, argument datatypes and return type....
s must be introduced. For example, a function defined in this way in one source file: int add(int a, int b) may be declared (with a function prototype
Function prototype

A function prototype in C or C++ is a declaration of a subroutine that omits the function body but does specify the function's name, arity, argument datatypes and return type....
) and then referred to in a second source file as follows: int add(int, int);

int triple(int x)

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 using the functionality. If the definition of the function ever changes, the programmer must remember to update all the prototypes scattered across the program, as well. This is necessary, because both C and C++ implementations need not diagnose all violations of what in C++ is called the one definition rule
One Definition Rule

The One Definition Rule is an important concept in the C++ programming language. It's defined in the ISO C++ Standard 2003, at section 3.2....
 (ODR). Actually most of them rely on a linker
Linker

In computer science, a linker or link editor is a computer program that takes one ormore object file generated by a compiler and combines them into a single executable program....
 to do this. The linker, however, is typically very limited in its knowledge of types used in a program. This leads to some ODR violations that can not be caught by the language implementation. As a result, it is the programmer's responsibility to keep all declarations that cross translation unit
Translation unit

In the field of translation, a translation unit is a segment of a text which the translator treats as a single cognitive unit for the purposes of establishing an equivalence....
 boundaries coherent. Manually searching for all declarations of the same external entity and verifying that they are compatible is a tedious task. (Note that C does not define the term one definition rule
One Definition Rule

The One Definition Rule is an important concept in the C++ programming language. It's defined in the ISO C++ Standard 2003, at section 3.2....
 — it is C++ specific. If declarations of the same entity in many C source files are different, however, the application will not function correctly and this results in undefined behavior, regardless of the naming of the rule being violated.)

To understand an ODR
Óðr

In Norse mythology, ??r or ??, sometimes angliziced as Odr, is a figure associated with the major goddess Freyja. The Prose Edda and Heimskringla, written in the 13th century by Snorri Sturluson, both describe ??r as Freyja's husband and father of her two daughters Hnoss and Gersemi....
 violation, consider the following (correct) example: /* File print-heading.c */ #include

void print_heading(void)

/* File main.c */ void print_heading(void);

int main(void)

The translation unit
Translation unit

In the field of translation, a translation unit is a segment of a text which the translator treats as a single cognitive unit for the purposes of establishing an equivalence....
 represented by the main.c source file references the print_heading function defined in another translation unit (print-heading.c). According to the rules of C99
C99

C99 is a modern dialect of the C programming language....
, programmers must declare an external function before the first use. To meet this requirement, the main.c file declares the function in the first line. This version of the program works correctly.

Later, the programmer who maintains the print-heading.c source file may decide to make the function more flexible and support custom headings. A possible implementation might look like this:

/* File print-heading.c */ #include

void print_heading(const char *heading)

If the programmer forgets to update the declaration in main.c, dire results can occur. The print_heading function expects an argument and accesses its value, however, the main function does not supply any value. Executing this program leads to undefined behavior: the application may print garbage, terminate unexpectedly or perform unexpectedly according to the platform that it is being executed on.

Why does this code compile and link successfully? This happens because the compiler relies on a declaration in main.c when compiling the main.c translation unit. And that declaration matches the form of the function. Later, when the linker combines the compiled main.c and print-heading.c translation units (in most implementations they are represented as main.o or main.obj files), it probably could detect the inconsistency — but not in C. C implementations reference functions by names at object and binary file levels, this does not include the return value or the argument list. The linker encounters a reference to print_heading in main.o and finds a suitable function in print-heading.o. At this stage, all information about function argument types is lost.

How do we therefore manage multiple declarations successfully? Header files provide the solution. A module's header file declares each function, object, and data type that forms 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:

/* File add.h */ #ifndef ADD_H #define ADD_H

int add(int, int);

#endif /* ADD_H */

(Aside: note that this header file uses an include guard
Include guard

In the C and C++ programming languages, an #include guard, sometimes called a macro guard, is a particular construct used to avoid the problem of double inclusion when dealing with the #include compiler directive....
.)

/* File triple.c */ #include "add.h"

int triple(int x)

This reduces the maintenance
Software maintenance

Software maintenance in software engineering is the modification of a software product after delivery to correct faults, to improve performance or other attributes, or to adapt the product to a modified environment ....
 burden: when a definition changes, 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.

/* File add.c */ #include "add.h"

int add(int a, int b)

Typically, programmers use header files to specify only interfaces
Interface (computer science)

Interface generally refers to an Abstraction_%28computer_science%29 that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide Polymorphism in object-orien...
, and usually provide as a minimum a small amount of documentation explaining how to use the components declared in the file. As in this example, the implementations of subroutines remain in a separate source file, which continues to be compiled separately. (One common exception in C and C++ is inline function
Inline function

In computer science, an inline function is a programming language construct used to suggest to a compiler that a particular function be subjected to inline expansion; that is, it suggests that the compiler insert the complete body of the function in every context where that function is used....
s, which are often included in header files because most implementations cannot properly expand inline functions without seeing their definitions at compile time
Compile time

In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time....
.)

Alternatives

Header files do not provide a monopoly solution to the problem of accessing identifiers declared in different files. They have the disadvantage that programmers may still find it necessary to make changes in two places (a source file and a header file) whenever a definition changes. Some newer languages (such as Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
) dispense with header files and instead use a naming scheme
Naming scheme

A naming scheme is a plan for naming objects. In computing, naming schemes are often used for objects connected into computer networks....
 that allows the compiler to locate the source files associated with interfaces and class implementations (but in doing so, restrict file-naming freedom). In such languages, the ODR problem is typically solved by two techniques: first, the compiler puts full information about types into compiled code and this information remains accessible even at run-time when the program executes. Second, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 and other modern languages have the ability to verify the number and types of arguments at method call. This does not come for free, however; it results in space and execution-time overhead that is not acceptable for some time-critical applications.

COBOL
COBOL

COBOL is one of the oldest programming languages still in active use. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
 and RPG IV have a form of include files called copybook
Copybook

Copybook in computer science A copybook is the section of code in a high-level computer programming language that is copied from a master copy and inserted in several different programs ....
s
. Programmers "include" these into the source of the program in a similar way to header files, but they also allow replacing certain text in them with other text. The COBOL keyword for inclusion is COPY, and replacement is done with a REPLACING ... BY ... clause.

See also

  • One Definition Rule
    One Definition Rule

    The One Definition Rule is an important concept in the C++ programming language. It's defined in the ISO C++ Standard 2003, at section 3.2....
  • Application Programming Interface
    Application programming interface

    An application programming interface is a set of subroutine, data structures, class and/or Protocol provided by library and/or operating system Service s in order to support the building of applications....
  • Interface Definition Language
  • #pragma once
    Pragma once

    In the C and C++ programming languages, #pragma once is a non-standard but widely supported C preprocessor designed to cause the current source file to be included only once in a single compilation....


External links