C Custom Header File
We can create our own custom header files in C. With the custom header file, we will be able to manage user-defined methods, global variables, and structures in a separate file, which can be used in different modules and in different programs.
Example:
In the following example, we will be creating a custom header file to “swap.h” in which the external function “swap” is defined.
main.c file
#include <stdio.h>
#include “add.h”
void main()
{
int a=5;
int b=10;
swap(&a, &b);
printf(“a=%d\n”, a);
pring(“b=%d\n”, b);
}
The add method is defined in swap.h, the two numbers are swapped.
swap.h
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
a=10
b=5
Notes
- Both the main.c and swap.h files should be in the same folder.
- For the custom header file, we use the terminology as “swap.h” instead of <swap.h>