12.8 – Creating and including header file in Turbo C

by subbu on December 21, 2013

How to create a header file in Turbo C:

In previousc session, we have discoursed how to configure Turbo C so that it could locate header files and library files automatically. In this session we will see how we can create a header file and include into any other program step by step

On Authors machine Directories are
Include Directory : c:\tc\include
Lib Directory : c:\tc\lib
Output Directory: c:\tc\works
Source Directory: c:\tc

Turbo C Directory settings

These details may change version to version, hoping you will set according to your machine

Step 1: Creating header file (manip.h)

  • Open the Turbo C IDE
  • Select File (Alt+F), New (N)
  • Type the functions of header file
/* c:\tc\include\manip.h */
int sum(int x,int y)
{
  return x+y;
}
int sub(int x,int y)
{
  return x-y;
}
int pro(int x,int y)
{
  return x*y;
}
float div(int x,int y)
{
 return (float)x/y;
}

Step 2: Save the header file in include folder with the extension .h

  • Select File (Alt+F), Save (S) or press F2
  • Save file as : c:\tc\include\manip.h

Saving header file

Step 3: Include the header file in a C program (demo.c) using #include statement

  • Select File (Alt+F), New (N)
  • Type the program, which will be using header file
#include<stdio.h>
#include<manip.h>
int main()
{
int a,b,sm,sb,pr;
float dv;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
sm=sum(a,b);
sb=sub(a,b);
pr=pro(a,b);
dv=div(a,b);
printf("Sum %d",sm);
printf("\nSubstraction %d",sb);
printf("\nProduct %d",pr);
printf("Division %f",dv);
return 0;
}

Step 4: Save the program with any name but with .c extension otherwise it would take .cpp extension

  • Select File (Alt+F), Save (S) or F2
  • Save file as : demo.c (By default as c:\tc\bin\demo.c)

Saving a C program

Step 4: Execute and see the output of program

Execute the program by using Ctrl+F9
See the output using Alt+F5

Executing a C program

Conclusion:

The preprocessor loads the header file “c:\tc\include\manip.h” and add to “demo.c” automatically in place of preprocessor statement #include<manip.h>
This we can see by looking at the expanded source of program “demo.i”

Including header file to source

Show Expanded Source
C:\TC\INCLUDE\manip.h 1: int sum(int x,int y)
C:\TC\INCLUDE\manip.h 2: {
C:\TC\INCLUDE\manip.h 3: return x+y;
C:\TC\INCLUDE\manip.h 4: }
C:\TC\INCLUDE\manip.h 5: int sub(int x,int y)
C:\TC\INCLUDE\manip.h 6: {
C:\TC\INCLUDE\manip.h 7: return x-y;
C:\TC\INCLUDE\manip.h 8: }
C:\TC\INCLUDE\manip.h 9: int pro(int x,int y)
C:\TC\INCLUDE\manip.h 10: {
C:\TC\INCLUDE\manip.h 11: return x*y;
C:\TC\INCLUDE\manip.h 12: }
C:\TC\INCLUDE\manip.h 13: float div(int x,int y)
C:\TC\INCLUDE\manip.h 14: {
C:\TC\INCLUDE\manip.h 15: return (float)x/y;
C:\TC\INCLUDE\manip.h 16: }
C:\TC\INCLUDE\manip.h 17:
demo.c 3: int main()
demo.c 4: {
demo.c 5: int a,b,sm,sb,pr;
demo.c 6: float dv;
demo.c 7: printf("Enter two numbers:\n");
demo.c 8: scanf("%d%d",&a,&b);
demo.c 9: sm=sum(a,b);
demo.c 10: sb=sub(a,b);
demo.c 11: pr=pro(a,b);
demo.c 12: dv=div(a,b);
demo.c 13: printf("Sum %d",sm);
demo.c 14: printf("\nSubstraction %d",sb);
demo.c 15: printf("\nProduct %d",pr);
demo.c 16: printf("Division %f",dv);
demo.c 17: return 0;
demo.c 18: }
demo.c 19:

Previous post:

Next post: