Header file

From Wikipedia, the free encyclopedia

In computer programming, especially in the C programming language or C++, a header file or include file is a file that provides declarations to other source files.

The problem: Programs are typically broken into pieces in order to make working with them easier. Related bits of code are collected into files, which can then be referred to from other files. However this leads to a problem when the code is collected into a runnable program, because all of the code must be compiled every time.

One solution to this problem is to have the system examine the modification dates of the files that make up the program and recompile files that need it; make does this. However this might tell the system to compile files that have had rather innocuous changes made to them, like fixing whitespace, or adding comments.

The header file provides a solution to this problem by separating out the parts that really have an effect when changed, from those that do not. Typically, header files are used to specify only interfaces, with the implementations left in the main code files.

In this case, they consist of "stub" definitions containing only the definition for the interfaces implemented in the main file, along with documentation for that code. This way the header file only changes if and when the very definition of the code changes, which is the time when other modules must also be recompiled as well. Now the system only has to look at the modification dates on the header files to see what needs to be re-built, which can result in significant savings in compilation time.

On the downside a header file means two files to modify when a change is made. Newer languages (like Java) typically dispense with header files and instead put a little more smarts into the system to see if the changes really have any effect.

When constructing libraries, header files are commonly used to describe the functionality in them, with no need of compiling the library at all, instead it is linked to the resulting object files at the end of the compilation.

Information from C/C++ header files is traditionally brought into other files using the C preprocessor's "#include" directive. To prevent the same header file from being included more than once, an include guard is put around the body of each header file.

Contents

[edit] C header file:

A C header file is a text file that contains pieces of code written in C programming language syntax, and whose name, by convention, ends with “.h” extension. A C header file ( also informally known as “.h file” pronounced dot-H-file ), is used inside a program by coding the “#include” preprocessor directive. The syntax is as follows:

#include <standard-library-header-file>
                 or
#include "user-defined-header-file"

Note the slight difference in the syntax when including a standard library header files and user defined header files.

In programs written in C, it is often desirable to separate function declarations, global, static and external variable declarations from the rest of the source code. This provides software developers with three major advantages:

  • Organizes the source code into well defined entities.
  • Separates interfaces and implementation of functionality.
  • Provides flexibility and better opportunity of reuse.

Header files in C (and C++) are meant exactly for this purpose.

[edit] Examples:

Imagine you are writing a program to convert a measurement in miles to kilometers. In order to accomplish this task, you write a function that does the job. Further assume that your implementation of the function is as follows:

programA.c:

#include <stdio.h>
const double FACTOR = 1.609344;

double cvtMileToKilom( double miles );

int main()
{
  double miles = 0;
  printf("Enter measurement in Miles:");
  scanf("%lf", &miles);
  printf("%f miles is %f kilometers.\n", miles, cvtMileToKilom( miles ) );
  return 0;
}

double cvtMileToKilom( double miles )
{
  return FACTOR * miles;
}

A few days later you decide to write a program that converts miles to meters. Further assume that you love your previously written code, and you must reuse it for your new project. You have two options. One option is to copy your code and paste it into your new program, like the following:

programB.c

#include <stdio.h>
const double FACTOR = 1.609344;

double cvtMileToMeter( double miles );

int main(void)
{
  double miles = 0;
  printf("Enter measurement in Miles:");
  scanf("%lf", &miles);
  printf("%f miles is %f meters.\n", miles, cvtMileToMeter( miles ) );
  return 0;
}

double cvtMileToKilom( double miles )
{
  return FACTOR * miles;
}

double cvtMileToMeter( double miles )
{
  return cvtMileToKilom( miles ) * 1000;
}

A better option is to separate your function declaration for cvtMileToKilom from its implementation. Suppose in your first project you structured your program as follows:

programA.h

#ifndef INCLUSION_GUARD_PROGRAM_A_H
#define INCLUSION_GUARD_PROGRAM_A_H

#define FACTOR ((double) 1.609344)

double cvtMileToKilom( double miles );

#endif // INCLUSION_GUARD_PROGRAM_A_H

Note, the use of the inclusion guard (in this case the INCLUSION_GUARD_PROGRAM_A_H) prevents multiple inclusion of the same header file.

programA.c

#include "programA.h"
double cvtMileToKilom( double miles )
{
  return FACTOR * miles;
}

This way all you need to do for your new project is to include your header file in the new program and compile all of your .c files:

programB.c

#include <stdio.h>
#include "programA.h"

double cvtMileToMeter( double miles );

int main()
{
  double miles = 0;
  printf("Enter measurement in Miles:");
  scanf("%lf", &miles);
  printf("%f miles is %f meters.\n", miles, cvtMileToMeter( miles ) );
  return 0;
}

double cvtMileToMeter( double miles )
{
  return cvtMileToKilom( miles ) * 1000;
}

The beauty of this method is that you don’t have to remember how you implemented cvtMileToKilom. All you need to be able to do is to use it, and the #include “programA.h” statement is all you need to access your previous work. No need for copy pasting! This is the basic example of software reuse.

Please note that the code segments provided above are in no way the best way to do the projects. Nor are they examples of good C programming style. Rather they attempt to illustrate the use of header files. There is still a lot of room for improvement. For instance, you could put the function definitions of cvtMiletoMeter and cvtMiletoKilom in a single .c file, for example “conversion.c”. Also put the prototypes of these two functions in one .h file, for example “conversion.h”. This way, you can add more and more functions that convert from one unit to another. Every time you want to do another conversion, you can use your library and the functions available in it.

The idea of header files is not limited to C. For example, interfaces in Java are analogous to C header files.

[edit] See also

[edit] External links