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
Here is an Example :
Save this code as "sum.h" in INCLUDE directory.
Now include the header file in the program like this: int sum(int num1,int num2){
return (num1+num2);
}
#include<stdio.h>
#include<conio.h>
#include<sum.h>
void main(){
clrscr();
printf("%d\n",sum(5,5));
getch();
}
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.
0 comments:
Post a Comment