- How can we create our own header file in C.
- How to create your own function library in C programming language like stdio.h
Here we will create a new header file called "myMath.h" and a function "int getNearestInteger(float)" that will convert a floating point number to nearest integer and return. You can add any number of functions in a header file. Utility functions are best candidates for getting included in a header file so that we can use them in multiple programs.
Benefits of creating your own header file having common utility functions.
- Code re-usability : If you added a function in a header file, then you don't have to type it again in any program where you want to use it. Just include your header file using #include preprocessor and call you function just like any other standard library function.
- Easy to maintain : Later, If you want to change the internal implementation of any function, then you have to modify only in one place(inside header file). You don't have to do any change in any of the client(programs who calls this function) of this function as long as the function prototype remains same.
Here are the steps to create your own header file
#include <stdio.h>
#include "myMath.h"
int main(){
float number;
printf("Enter an floating point number\n");
scanf("%f", number);
printf("Nearest Integer of %f is %d\n", number,
getNearestInteger(number));
return 0;
}
Output
Enter an floating point number
2.3
Nearest Integer of 2.3 is 2