Header Files in C
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>
#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.
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:
Header Files in C 1
• First, we will write our own C code and save the file with .h extension. Below is
the example to create our header file:
// function to multiply two numbers and return the result.
int multiply(int a, int b)
{
return (a*b);
}
Suppose the name of the file is multiply.h.
• The second step is to include our header file in our main program.
#include<stdio.h>
// including header file
#include "multiply.h"
int main()
{
int a =1, b = 2; // definition of two numbers
// function defined in multiply.h header file to calculate
printf("Result of multiplication is : %d",multiply(a, b))
return 0;
}
Some of the header files are given below:
Header Files in C 2
Header Files in C 3
Header Files in C 4
Header Files in C 5
Example 1:
// 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] = "javatpoint";
Header Files in C 6
// 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 t
int length = strlen(str2);
printf("\nThe length of the string str2 is : %d", length);
return 0;
}
ASCII(American Standard Code for Information
Interchange) Values:
Header Files in C 7
Example: Program to get the ASCII value of the capital
letters
#include <stdio.h>
int main()
{
// declare local variable
int caps;
// use for loop to print the capital letter from A to Z
for ( caps = 65; caps < 91; caps++)
{
printf (" \n The ASCII value of %c is %d ", caps, caps);
Header Files in C 8
}
return 0;
}
Header Files in C 9