In C programming language, a header file is a file with the extension .h that contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that come with the compiler. Header files offer features like library functions, data types, macros, etc. by importing them into the program with the help of a preprocessor directive #include
. These preprocessor directives are used to instruct the compiler that these files need to be processed before compilation.
Header files serve two purposes. First, including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. Second, header files eliminate the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program.
Header files can contain function prototypes or function declarations, whereas the source code contains the constants, macros, system-wide global variables. Both user-defined and system-defined header files can be included in a program with the help of using preprocessing directive #
. There are two forms of including header files: #include <header.h>
and #include "header.h"
. The former is used to include system-defined header files, while the latter is used to include user-defined header files.
If a header file happens to be included twice, the compiler will process its contents twice, and it will result in an error. The standard way to prevent this is to enclose the entire real contents of the header file in a conditional as follows:
#ifndef HEADER_H
#define HEADER_H
/* contents of header file */
#endif
In the above condition, if the header is included, then it will not be included again. If the header file is not included, then the header file will be included. The construct ifndef
will become false if the header file is included twice, and the preprocessor will skip all the contents of the file, and the compiler will ignore the second declaration of the header file.
In summary, header files in C are files with the extension .h that contain C function declarations and macro definitions to be shared between several source files. They offer features like library functions, data types, macros, etc. by importing them into the program with the help of a preprocessor directive #include
. Header files serve two purposes: to avoid copying the same code into multiple source files and to eliminate the labor of finding and changing all the copies. There are two types of header files: user-defined and system-defined. If a header file is included twice, it will result in an error, and the standard way to prevent this is to enclose the entire real contents of the header file in a conditional.