Header Files in C

At the very beginning of every C program, we write, the first thing we do is include the header files. What basically are they and what is their purpose, we'll find out in this article.

Header Files in C
Header Files in C


What are Header files?

These are special files that contain several commands pre-defined or declared within them, such commands help in the overall execution of any program. If in any program we don't "call" the header files, all those commands will become invalid and won't do their dedicated tasks. They lose their very identity.
Thus, right at the top of any program, header files are "included".

Such files can easily be identified as they end with a ".h" extension. Also to fetch them in any program code one has to use a pre-processor directive "#include". By default, you'll have to add #include<stdio.h> before every program that you do in C compiler. However, in C++ that happens with #include<iostream.h> header file.

Including a header file implies that one is using the content of that header file in their source program. The syntax of using these header files is:

#include<file> or, #include "file"

If you just imagined they are different by looks and that only, you're wrong.

Angular braces(< >) method searches for that file-name in the standard list of system directories or in the compiler's directory of header files.

Quotes(" ") method is used for user-defined custom header files for your program. It helps in searching for the file(s) within the directory that contains the current file.

How Header files are included?

The #include pre-processor directive works by going through the C pre-processors for scanning any particular file. Until that file is called the rest of the code will be held before execution.

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

char *demo (void);

and a main program called test.c that uses the header file, as follows:

int a;
#include "techgyd.h"
int main (void)
   puts(demo ());

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

int x;
char *demo (void);
int main (void)
   puts(demo ());

How many header files can be added in a program?

There is no such rule about including single header file in a program. If you apply commands in body of the program that are not available in default header file then you'll need to add other header files too.

Header File Once Only

If a particular header file needs to be included twice, the compiler will compile its contents twice and it will end in an error. The best way to prevent this is to enclose the entire real contents of the file in a conditional, like this:


contents of the header file

When the header is included again, the conditional will be false, because HEADER_FILE is defined. The preprocessor will skip over the entire contents of the file, and the compiler will not read it twice.

That's enough for header files in this article. There's even feature of custom header files that I might put up soon.

Books I Prefer:


I hope this article was helpful to understand header files in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.