What are Header Files in C Programming?

In this module, we will talk about what are Header files in C Programming, their importance, and their uses. Till now, we have seen various concepts like Hello World Program in C, and Input-Output Statement in C, and in all these, we have seen that while writing a program we need a header file, without that we can’t proceed towards the program or it will simply throw an error in the console like header file not declared or a particular function is invalid.

So, Let’s see in detail the importance and uses of header files in C programming.

What are Header Files in C Programming?

In C Programming language, there are many predefined functions available which make the program very easy to write, and these predefined functions are available in the C library, to use all these functions you need to declare a particular header file because the header file then makes a call to the C library and uses that particular function in your program. You can include the header files by C Pre-processing directives i.e., “#include”, and must contain a “.h” extension at the end.

What are Header Files in C

For example, #include<stdio.h>, this is the header file which stands for standard input-output function, like this is the basic header file which is necessary for writing any program in C, as it allows you to perform input and output operations in C, like scanf( ) and printf( ) functions, and #include is the pre-processor directive.

By including the header file, we can use all its functions and contents in our programming.

There are 2 types of header files in C Programming

  • Pre-existing Header Files in C Programming
  • User-defined Header Files in C Programming

Pre-existing Header Files in C Programming

These files are the files that are already available in the C compiler, we have to just import them, whenever we want to use them.
For example: #include <File_name.h>

User-defined Header Files in C Programming

When users want to define their own header and that can be simply imported by using “#include”.
For example: #include “File_name.h”

So, these are the two ways through which we can use the header file in a C program. The pre-processor directive i.e., “#include” is only responsible for telling the compiler that the header file needs to be processed before the actual compilation and must include all the necessary functions and data for the program to use. The user-defined data type searches for a file name in the current directory and then finds it and calls them in the program.

What are the different types of header files available in C Programming?

There are many different types of header files available and used for different purposes, which makes our program easier, let’s see some of them in detail.

  1. #include <stdio.h>: It is the basic header that must be included while writing any program in C, as it allows you to perform input and output operations using scanf( ) and printf( ) functions.
  2. #include <math.h>: This header file allows us to perform different types of mathematical functions like:
    sqrt(): It is used to find the square root of a particular number, if you want to find the square root of n, then it should be declared as sqrt(n);
    pow(): This function is used to compute the power of a particular number like 23 = 8.
    log2(): It is used to find the log of the number to the base 2.
  3. #include <string.h>: This function allows us to perform various functionalities related to the manipulation of string like:
    strlen(): It is used to find the length of a particular string.
    strcmp(): It is used to compare the two strings, it returns true or 1 if both strings is the same and return false or 0 if both strings are not the same.
    strcpy(): This function is used to copy one string to another variable.
  4. #include <stdlib.h>: This stands for the standard library, which contains general utility functions.
  5. #include <time.h>: It is used for date and time functions.
  6. #include <ctype.h>: It includes various character type handling functions.
  7. #include <conio.h>: It stands for console input-output operations, like it does not belong to C standard library, but is used for performing console input-output operations.
  8. #include <float.h>: this header file is limited to float types only.

So, these were some header files available with their meanings, Let’s see one example to get clearer:

#include <stdio.h>
#include <math.h>
int main()
{
int num1 = 25;
int num2 = 3;
int t=0, m=0;
t = sqrt(num1);
m = pow(t, num2);
printf("t = %d\n ", t);
printf("m = %d ", m);
return 0;
}

The output of the above program is:

t = 5
m = 125

In the above program, I have used two header files i.e., #include <stdio.h> and #include <math.h> for performing various mathematical operations like having computed the square root of 25 and then calculating 5 to the power 3 using pow( ) functions.

I hope, you all liked the What are Header Files in C Programming module and got excited by knowing this particular concept and must be waiting for another module for a more exciting and interesting concept of C Programming.

Until then, keep learning, Practice Coding!