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.
Syntax:
#include <file>
Types of Header Files in C
There are 2 types of header files such as:-
Standard Library Header Files:– These are pre-existing header files which are available in C.
User-Defined Header Files:- The header files which are defined by the user then they are called user-defined header files. The #define directive is used to define a header file.
Different header files in C:-
You can use these standard C library functions by declaring header files. Different header files include different functions and different operations.
Below is the list of header files in C:-
Header File | What is does |
---|---|
stdio.h | Mainly used to perform input and output operations like print(), scanf(). |
string.h | Mainly used to perform string handling operations like strlen(), strcmp() etc. |
conio.h | With this header file, you can execute console input and output operations. |
stdlib.h | Mainly used to perform standard utility functions like malloc(), calloc() etc. |
math.h | Mainly used to perform mathematical operations like sqrt(), pow() etc. |
ctype.h | Used to perform character type functions like isdigit(), isalpha() etc. |
time.h | Used to perform operations related to date and time. |
errno.h | For performing error handling operations such as errno() to indicate the errors. |
Examples:-
/ C program to understand the usage of header file. #include <stdio.h> #include <string.h> #include <math.h> int main() { char str1[10] = "hello"; char str2[10] = "Topperworld"; // function defined in math.h header file long int a = pow(3, 3); printf("The value of a is %d", a); // function defined in string.h to calculate the length of the string int length = strlen(str2); printf("\nThe length of the string str2 is : %d", length); return 0; }
Output:
The value of a is 27
The length of the string str2 is : 11