C Header Files
What is a header file?
A header file is a source file that has the .h extension. Header files contain the function prototypes or function declaration, whereas the source code contains the constants, macros, system-wide global variables. Whenever we require the definition of a function, then we simply include that header file in which function is declared.
There are two types of header files defined in a program:
- System defined header file: The header file which is predefined is known as a system defined header file.
- User-defined header file: The header file which is defined by the user is known as a user-defined header file.
Both the user-defined and system-defined header file can be included in a program with the help of using preprocessing directive (#). These preprocessor directives are used to instruct the compiler to process these files before compilation. There are two forms of including header file:
- #include “file”
There is a difference between the header files given above. If the header file is defined within the predefined source path, we can specify the header within the angular brackets. If the header file is not defined within the predefined source path then we can specify the full path of the header file within the double-quotes.
Let’s suppose that header.h header file contains the following declaration:
Definition of program.c source file is given below:
Compiler would replace the definition of header.h header file as shown below:
Once only headers
If the header file in a source code is included twice, then it leads to an error, i.e., multiple declarations. To get rid of this problem, there is a standard way of enclosing the real content of the header file in a conditional as follows:
In the above condition, if the header is included, then it will not be included again. If the header file is not included, then the header file will be included. The construct “ifndef” will become false if the header file is included twice and the preprocessor will skip all the contents of the file, and the compiler will ignore the second declaration of the header file.
Let’s understand the above scenario through an example.
The following is the source code including the header file:
Create your own header file
Header file is used to avoid writing large and complex code. When we create our own header file then we can simply use wherever we want. It enhances code readability and functionality.
The following are the steps to create our own header file:
- First, we will write our own C or C++ code and save the file with .h extension. Below is the example to create our header file:
Suppose the name of the file is multiply.h.
- The second step is to include our header file in our main program.
Some of the header files are given below:
- #include<stdio.h>: It is used for performing input and output operations with the help of using printf() and scanf() function.
- #include<string.h>: It is used for performing string related functionalities like strlen(), strcmp(), etc.
- #include<iostream>: It is used to perform input and output operations with the help of using cin and cout objects.
- #include<math.h>: This header file contains some predefined math functions that perform mathematical operations, such as sqrt(), log2(), pow(), etc.
- #include<iomanip.h>: It contains the definition of set() and setprecision() function to limit the decimal places in variables.
- #include<signal.h>: It contains the definitions of functions that perform the signal handling functions such as signal(), raise().
- #include<errno.h>: It performs the error handling related operations like errno(), strerror(), perror(), etc.
Let’s understand the usage of above header files through an example.
In the above code, we have observed the usage of header files like string.h in which strlen() function is defined, and pow() function is defined in math.h header file.