C Header Files

C language is famous for its different libraries and the predefined functions pre-written within it. These make programmer's effort a lot easier. In this tutorial, you will be learning about C header files and how these header files can be included in your C program and how it works within your C language.

What are the Header Files

Header files are helping file of your C program which holds the definitions of various functions and their associated variables that needs to be imported into your C program with the help of pre-processor #include statement. All the header file have a '.h' an extension that contains C function declaration and macro definitions. In other words, the header files can be requested using the preprocessor directive #include. The default header file that comes with the C compiler is the stdio.h.

Including a header file means that using the content of header file in your source program. A straightforward practice while programming in C or C++ programs is that you can keep every macro, global variables, constants, and other function prototypes in the header files. The basic syntax of using these header files is:

Syntax:
#include <file>

or

#include "file"

This kind of file inclusion is implemented for including system oriented header files. This technique (with angular braces) searches for your file-name in the standard list of system directories or within the compiler's directory of header files. Whereas, the second kind of header file is used for user-defined header files or other external files for your program. This technique is used to search for the file(s) within the directory that contains the current file.

How include Works

The C's #include preprocessor directive statement exertions by going through the C preprocessors for scanning any specific file like that of input before abiding by the rest of your existing source file.  Let us take an example where you may think of having a header file karl.h having the following statement:

Example:
char *example (void);

then, you have a main C source program which seems something like this:

Example:
#include<stdio.h>

int x;
#include "karl.h"
int main () 
{
   printf("Program done");
      return 0;
}

So, the compiler will see the entire C program and token stream as:

Example:
#include<stdio.h>

int x;
char * example (void);
int main () 
{
   printf("Program done");
      return 0;
}

Writing of Single and Multiple uses of Header files

You can use various header files based on some conditions. In case, when a header file needs to be included twice within your program, your compiler will be going to process the contents inside it - twice which will eventually lead to an error in your program. So to eliminate this, you have to use conditional preprocessor directives. Here's the syntax:

Syntax:
#ifndef HEADER_FILE_NAME
#define HEADER_FILE_NAME
   the entire header file
#endif

Again, sometimes it's essential for selecting several diverse header files based on some requirement to be incorporated into your program. For this also multiple conditional preprocessors can be used like this:

Syntax:
#if FIRST_SYSTEM
   #include "sys.h"
#elif SEC_SYSTEM
   #include "sys2.h"
#elif THRID_SYSTEM
   ....
#endif

Here are few other related articles for you to read: