
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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".