How to write your own header file in C?\n


Here we will see how to create own header file using C. To make a header file, we have to create one file with a name, and extension should be (*.h). In that function there will be no main() function. In that file, we can put some variables, some functions etc.

To use that header file, it should be present at the same directory, where the program is located. Now using #include we have to put the header file name. The name will be inside double quotes. Include syntax will be look like this.

#include”header_file.h”

Let us see one program to get the idea.

Example

int MY_VAR = 10;
int add(int x, int y){
   return x + y;
}
int sub(int x, int y){
   return x - y;
}
int mul(int x, int y){
   return x * y;
}
int negate(int x){
   return -x;
}

Example

#include <stdio.h>
#include "my_header.h"
main(void) {
   printf("The value of My_VAR: %d\n", MY_VAR);
   printf("The value of (50 + 84): %d\n", add(50, 84));
   printf("The value of (65 - 23): %d\n", sub(65, 23));
   printf("The value of (3 * 15): %d\n", mul(3, 15));
   printf("The negative of 15: %d\n", negate(15));
}

Output

The value of My_VAR: 10
The value of (50 + 84): 134
The value of (65 - 23): 42
The value of (3 * 15): 45
The negative of 15: -15

Updated on: 30-Jul-2019

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements