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



What are the Header Files

Header files are additional files in a C language containing definitions of different functions and their associated variables that need to be imported into a C program with the help of a preprocessor #include statement. All the header files have a '.h' extension that contains C function declarations and macro definitions. The default header file that comes with the C compiler is the stdio.h.

Including a header file means using the content of the 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. The second kind of header file is used for user-defined or external files. This technique is used to search for the files within the directory that contains the current file.

How include Works

C's #include preprocessor directive statement tries to go through the C preprocessors to scan for a specific file, such as input, before following the rest of your existing source file. Let us take an example where you may think of having a header file, example.h has 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 "example.h"
void main () 
{
    printf("Program execution completed.");
}

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

Example:

#include<stdio.h>

int x;
char * example (void);
void main () 
{
    printf("Program execution completed.");
}

Writing of Single and Multiple uses of Header files

You can use various header files based on some conditions. If a header file needs to be included twice in your program, your compiler will process the content inside it twice, which will eventually cause 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 to select several diverse header files based on some requirement to be incorporated into your program. For this also, you can use multiple conditional preprocessors like this:

Syntax:

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


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram