Monday, 14 April 2014

Things you will learn :

  • What are Headers
  • How to create Headers
  • Why should i create Headers
  • How to include header in your program

What are Header files?

  Header files are similar to the library files in Java. They are created once and can be used in any program in which they are included. Actually you define your functions in the header files to use them again and again without typing the whole code again. All you have to do is to include them in your program first. The most common header files in C/C++ is"conio.h" inside this header some functions are defined which are used to perform actions on console screen.

Create Header Files

Creating a Header file is not difficult at all here is the step by step method:
  • Open Notepad
  • Create your function definations there
  • Open the folder where you compiler saves its Header Files
  • For turbo C++ its in C drive --> TurboC++ --> Disk --> TurboC3 --> INCLUDE 
  • Save the file with ".h" extension  in INCLUDE folder
Congrats You have created a header file. Now you just have to include your header file in your program and check if its working in the way you want it to. You can include the file like this #include<headerfilename.h.>. There should be no errors if you followed the step by step method above.

Here is an Example :

Save this code as "sum.h" in INCLUDE directory.
 int sum(int num1,int num2){  
 return (num1+num2);  
Now include the header file in the program like this:
  void main(){  

Explanation : 

In the header file i have defined a function which takes two numbers as parameters and returns their sum. And in my program i just included header file(#include<sum.h>) and called the function which i defined earlier in my header file(printf("%d\n",sum(5,5))). The output is 10.

Today i discussed probably the easiest way of creating the header files. If you know even easier method than that. Share it with others in the comments below. And if you like this post Share it with your friends and Subscribe for more :)


Post a Comment

Subscribe to RSS Feed Follow me on Twitter!