Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



C-HeaderFiles

A header file is a file with extension .h which contains C function declarations and macro definitions and to be shared between several source files. There are two types of header files basically: the files that the programmer writes and the files that come with your compiler.

You request the use of a header file in your program by including it, with the C preprocessing directive #include like you have seen inclusion of stdio.h file which is a header file, which comes along with your compiler.

Including a header file is similar to copying the content of the header file but we do not do it because it will be very much error-prone and it is not a good idea to copy the content of header file in the source files, specially if we have multiple source file comprising our program.

A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in header files and include that header file wherever it is required.

Include Syntax

Both user and system header files are included using the preprocessing directive #include. It has following two forms:

#include <file.h>

This form is used for system header files. Searches for a file named file.h in a standard list of system directories. We can prepend directories to this list with the -I option while compiling your source code.

#include "file.h"

This form is used for header files of your own program. It searches for a file named file.h in the directory containing the current file. We can prepend directories to this list with the -I option while compiling your source code.

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 source file. Output from the preprocessor contains the output already generated, followed by output resulting from the included file, followed by output that comes from the text after the #include directive. Example, if you have a header file named header.h as follows:

char *test (void);

and given a main program called program.c that uses the header file header.h:

int x;
#include "header.h"
int main (void)
{
puts (test ());
}

the compiler will see the token stream as:

int x;
char *test (void);
int main (void)
{
puts (test ());
}
Once-Only Headers

If a header file happens to be included twice in the same program, the compiler will process its contents twice and will result an error. Standard way to prevent this is to enclose the entire real contents of the file in condition, like:

#ifndef HEADER_FILE
#define HEADER_FILE
the entire header file file
#endif

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

Computed Includes

Sometimes it is necessary to select one of several different header files to be included into our program. They might specify configuration parameters to be used on different sorts of operating systems, for instance. We could do this with a series of conditionals as shown:

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

But as it grows larger, it becomes tedious, instead of that the preprocessor offers the ability to use a macro for the header name. It is called a computed include. Instead of writing header name as the direct argument of #include, you simply put a macro name there:

#define SYSTEM_H "system_1.h"
...
#include SYSTEM_H

SYSTEM_H will be expanded, and preprocessor will look for system_1.h as if #include had been written that way originally. The SYSTEM_H could be defined by our Makefile with a -D option.



Related Videos