Explain the custom header files in C language


Problem

Can the user create his/her own custom header files in the C language? If yes, how can we access the user-defined header files?

Solution

Yes, the user can create his/her own custom header files in C.

It helps you to manage the user-defined methods, global variables, and structures in a separate file, which can be used in different modules.

Let’s see an example of how to create and access custom header files −

Example

Given below is the C program to call an external function named swap in the main.c file.

#include<stdio.h>
#include"swaping.h" //included custom header file
void main(){
   int a=40;
   int b=60;
   swaping (&a,&b);
   printf ("a=%d
", a);    printf ("b=%d
",b); }

Swapping method is defined in swapping.h file, which is used to swap two numbers by using a temporary variable.

This code is saved by using swapping.h in same folder, where the main.h is saved.

void swapping (int* a, int* b){
   int temp;
   temp = *a;
   *a = *b;
   *b = temp;
}

Note

  • Header file have .h file extension.

  • Both files swapping.h and main.c must be in the same folder.

  • To differentiate between predefined and custom-defined header files, instead of <swapping.h>, we have written #include "swapping.h".

Updated on: 08-Mar-2021

439 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements