A header file is a file containing C declarations and macro definitions to be shared between several source files.
Header can be the system header file or it can be created by user.
Syntax for System Header File :
#include <file>
Example :
#include <stdio.h>
Explanation :
It searches for a file named ‘stdio.h’ in a standard list of system directories.
Syntax for User defined header file :
#include "file"
Explanation :
It searches for a file named 'file' in the directory containing the current file.
Example :
Create a header file in demoheader.h, that contains add() function
int add(int a,int b)
{
return(a+b);
}
Main program in demo.c
#include<stdio.h>
#include"demoheader.h"
void main()
{
int num1 = 20, num2 = 30, num3;
num3 = add(num1, num2);
printf("Addition of Two numbers : %d", num3);
}
Output :
Addition of Two numbers : 50
Explanation :
The compiler will see the same token stream as it would, if demo.c read.
#include<stdio.h>
int add(int a,int b)
{
return(a+b);
}
void main()
{
int num1 = 20, num2 = 30, num3;
num3 = add(num1, num2);
printf("Addition of Two numbers : %d", num3);
}
NOTE :
- A header file is a file with extension .h
- To use header file in the program by including it with the C preprocessing directive #include
No comments:
Post a Comment