C Language: Header Files

Learn Details About Header Files in C Programming Language.

Header Files in C

C programming language offers various exciting and useful features and functionalities to the programmers. Almost every programmer uses the C language to develop software, games, and many others.

Syntax of the C is very easy to understand and also C is easy to implement. In all C programs, Header files play an important role. You cannot compile or execute any programs without specifying a header file.

Let us learn about Header files in C.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

What is a Header File in C?
A header file is a file that has the .h extension. In C, all header files must have the .h extension. A header file contains:-

Function Declaration
Data Type Definitions
With the help of header file, you can use the above mentioned features in your program code. Before using, you have to include the header file into your program and you can do this with the help of a preprocessor directive called #include.

These preprocessor directives tell the compiler that before compilation, the header files need to be processed. You have to stdio.h header file in each and every C program code.

The header file is used for standard input and output operations such as reading data from the user using scanf() function and printing output on the screen using printf() function.

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.
Syntax of Header File in C
There are two ways to include a header file in your program:-


The header file is enclosed within angular brackets. This is the most common way of defining a header file.



This is enclosed within double-quotes. This way, you can define user-defined header files.


NOTE:- You cannot include the same header file in the same program twice.

Working of C header files:-
With the help of #include preprocessor directive, you can include a header file in the program code. This preprocessor directive tells the compiler that the header file needs to be processed before compilation.

It contains all the important function declarations, macro definitions, and data type definitions.

Include Operation in C:-
Let’s say that you have a header file that contains the following declaration.

char *testing();
Below is the main.c program which uses this header file.


int main() {
return 0;
The compiler will change the definition of the header.h file as below:-

char *testing();
int main () {
puts (testing());
return 0;
Once Only headers in C:-
If you include the header file in a program code twice then it will give you an error. So, to avoid this type of problem, you will have to enclose the contents of the header file in a conditional like below:-






if header

    #include "myheader.h"

elif header2

    #include "myheader2.h"

elif header3



In the above, if the header file is not included then it will be included but if it is included then it won’t be included again. The #ifndef construct will result in false if a header file is included twice.

Example:- Only once headers

ifndef header_file

define header_file

int x = 23;


Below is the source code which uses the header file:-


include “header.h”

include “header.h”

int main()
printf(“TechVidvan Tutorial: Header files in C!\n”);
printf(“Value of x: %d”, x);
return 0;
The above example will not throw an error because we have specified the ifndef construct. With the help of ifndef, If the header file is included then it won’t be included again.

How to create your own header file:-
Apart from the pre-existing header files in C, you can create your own header file too. With the help of header files, you can avoid writing long and complex codes in your program. It also enhances the readability and functionality of the program code.

Let us take an example on how to create your own header file through steps.


Write your own code in the .h extension file. Because you are creating a header file which you will use in your program code.

int fact(int num)
int count, fact=1;
for(count=1; count<=num; count++)
return fact;
Name the above code as fact.h.


Include the fact.h header file in your program code. You can do this in two ways:-




Then compile and run the program code.



int main()
printf(“TechVidvan Tutorial: Create your own header file!\n”);
int n=6;
printf(“The factorial of %d is: %d!”,n,fact(6))
return 0;

TechVidvan Tutorial: Create your own header file!
The factorial of 6 is: 720!
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 it 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.
assert.h Used to verify assumptions made by a program and print a message if the assumption is false using functions like assert() etc.
locale.h Mainly used to perform localization like functions such as setlocale() etc.
signal.h Mainly used to perform signal handling functions such as signal(), raise() etc.
setjmp.h Used to perform jump functions.
stdarg.h Used to perform standard argument functions like va_start() etc.
errno.h For performing error handling operations such as errno() to indicate the errors.
Example:- Header files in C




int main()
char s1[20] = “TechVidvan”;
char s2[15] = “Tutorial”;
printf(“TechVidvan Tutorial: Example of header files in C!\n\n”);
int num = pow(4, 2); // pow() function from math.h header file!
printf(“Value of num: %d\n”, num);
int len = strlen(s1); // strlen() function from string.h header file!
printf(“String s1’s length is: %d”, len);
return 0;

TechVidvan Tutorial: Example of header files in C!

Value of num: 16
String s1’s length is: 10

In the above example, we have included two header files such as math.h and string.h. And with the help of these files, we did some operations on the variables like length of the string s1 and calculating the power of the num variable.

What are Standard library functions in C?
These functions are also known as built-in functions in C. And these functions are stored in a common collection called a library. Each function provides a different operation.

With the help of these standard functions, a programmer can get the desired output of his/her program easily. During the time of designing compilers, these library functions are created.

In this tutorial, we learnt about what C header files are and how you can use it in your program code. We discussed the types of header files in C. We also talked about the working of header files.

Then we discussed how you can create your own header files. We discussed different header files in C. Without a header file, you cannot run or compile your program code. In C, all program codes must include the stdio.h header file.

Categories: C Tutorials
Tags: C header files, Header files in C, Standard Library Functions in C

Leave a comment