Create and Use Your Own Header File in C Programming


Levels of difficulty: / perform operation:

Step1 : Make header file

Example(write only function definition as you write in General C Program)

int add(int a,int b)
{
return(a+b);
}


Step 2 : Save Code

Save Above Code with [.h ] Extension .
Let name of our header file be myfirstheader [ myfirstheader.h ]


Step 3 : Write Main C Program To Use header file

#include<stdio.h>
#include "myfirstheader.h"
 
void main() {
   int num1 = 10, num2 = 10, num3;
   num3 = add(num1, num2);
   printf("Addition of Two numbers : %d", num3);
}


Note

  • Instead of writing < myfirstheader.h> use this terminology “myfirstheader.h”
    All the Functions defined in the myfirstheader.h header file are now ready for use .
  • Directly call function add(); [ Provide proper parameter and take care of return type ]
  • While running your program precaution to be taken : Both files should be in same folder.