Sunday 25 January 2015

How to add a user defined function to a library function:

Here we are going to create a function for factorial of a the given number:


Step 1:
First create the definition of the program and save it with name fact .c
 Int factorial(int num)
{
int  f=1,I;
For(i=1;i<=num;i++)
{  
            F=f*I;
}
return (f);
}


Step 2:
Compile “fact.c” file by using Alt + F9 keys (in turbo C).


step 3:
“fact.obj” file would be created which is the compiled form of “fact.c” file.


Step 4:
Then for adding your function to the library function type the code given below:
c:\> tlib math.lib + c:\ fact.obj
+ means adding c:\fact.obj file in the math library.
We can delete this file using – (minus){
c:\> tlib math.lib - c:\ fact.obj}.


Step 5:        
Create a file “fact.h” & declare prototype of fact() function like below.
int factorial (int num);
Now fact.h file containing prototype of function “factorial”.


Step 6:
Let us see how to use our newly added library function in a C program.
# include <stdio.h>
// Including our user defined function.
# include “c:\\factorial.h”    
int main ()
{
       int f,n;
        prinntf(“enter any number”);   //message to the user

        scanf(“%d”,&n);                //receiving input from user

                                      // calling function from library
       f = factorial (n);

       printf ("factorial = %d \n", f);

        return 0;
}


No comments:

Post a Comment