Header Files In C Programming Language


Header Files Definition

A header file is a file containing C declarations and macro definitions (see Macros) to be shared between several source files. You request the use of a header file in your program by including it, with the C preprocessing directive ‘#include’.

Header file is a file that contains function declaration and macro definitions for C in-built library functions.

All C standard library functions are declared in many header files which are saved as file_name.h .

#include Syntax:

The user and the system header files both are included using the preprocessing directive #include.

  #include <file>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option while compiling your source code.


  #include "file"

This form is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to this list with the -I option while compiling your source code.

When we include header files in our C program using “#include <filename.h>” command, all C code of the header files are included in C program. Then, this C program is compiled by compiler and executed.

Include Operation

The ‘#include’ directive works by directing the C preprocessor to scan the specified file as input before continuing with the rest of the current file.

The output from the preprocessor contains the output already generated, followed by the output resulting from the included file, followed by the output that comes from the text after the '#include' directive.

For Example: if you have a header file header.h as follows,

  char *test (void);

and a main program called program.c that uses the header file, like this,

 int x;
 #include "header.h"

 main (void)
  puts (test ());

the compiler will see the same token stream as it would if program.c read

 int x;
 char *test (void);

 main (void)
  puts (test ());

Included files are not limited to declarations and macro definitions; those are merely the typical uses.

Any fragment of a C program can be included from another file. The include file could even contain the beginning of a statement that is concluded in the containing file, or the end of a statement that was started in the including file.

Once-Only Headers

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time.

The standard way to prevent this is to enclose the entire real contents of the file in a conditional, like this.

 /* File foo.  */
 #ifndef FILE_FOO_SEEN
 #define FILE_FOO_SEEN

 the entire file

 #endif /* !FILE_FOO_SEEN */

This construct is commonly known as a wrapper #ifndef. When the header is included again, the conditional will be false, because FILE_FOO_SEEN is defined. The preprocessors will skip over the entire contents of the file, and the compiler will not see it twice.

Computed Includes

Sometimes it is necessary to select one of several different header files to be included into your program. They might specify configuration parameters to be used on different sorts of operating systems, for instance.

You could do this with a series of conditionals,

 #if SYSTEM_1
 # include "system_1.h"
 #elif SYSTEM_2
 # include "system_2.h"
 #elif SYSTEM_3

That rapidly becomes tedious. Instead, the preprocessors offers the ability to use a macro for the header name. This is called a computed include. Instead of writing a header name as the direct argument of ‘#include’, you simply put a macro name there instead:

 #define SYSTEM_H "system_1.h"
 #include SYSTEM_H

looks for a file named a\"b. CPP searches for the file according to the rules for double-quoted includes.

System Headers

The header files declaring interfaces to the operating system and runtime libraries often cannot be written in strictly conforming C. Therefore, GCC gives code found in system headers special treatment.

Macros defined in a system header are immune to a few warnings wherever they are expanded. This immunity is granted on an ad-hoc basis, when we find that a warning generates lots of false positives because of code in macros defined in system headers.