Adding user defined functions in C Library

Adding user-defined functions in C Library

In this article, I am going to discuss the Adding user-defined functions in C Library with an example. Please read our previous articles, where we discussed the Recursive Functions in C.

How to Add user-defined functions in C Library?
  1. Do you know that we can add our own user-defined functions in the C library?
  2. Yes. It is possible to add, delete, modify, and access our own user-defined function to or from the C library.
  3. The advantage of adding a user-defined function in C library is, this function will be available for all C programs once added to the C library.
  4. We can use this function in any C program as we use other C library functions.
  5. In the latest version of GCC compilers, compilation time can be saved since these functions are available in the library in the compiled form.
  6. Normal header files are saved as “file_name.h” in which all library functions are available. These header files contain source code and this source code is added in main C program file where we add this header file using “#include <file_name.h>” command.
  7. Whereas, precompiled version of header files are saved as “file_name.gch”.
Steps for adding our own functions in C Library:

STEP 1: For example, below is a sample function that is going to be added to the C library. Write the below function in a file and save it as “addition.c”

addition(int i, int j)
    int total;
    total = i + j;
    return total;

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

STEP 3: “addition.obj” file would be created which is then compiled form of “addition.c” file.

STEP 4: Use the below command to add this function to library (in turbo C).
c:\> tlib math.lib + c:\ addition.obj
+ means adding c:\addition.obj file in the math library.
We can delete this file using – (minus).

STEP 5: Create a file “addition.h” & declare prototype of addition() function like below.
int addition (int i, int j);

Now, addition.h file contains a prototype of the function “addition”.

Note: Please create, compile, and add files in the respective directory as directory name may change for each IDE.

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:\\addition.h”     
int main ()
     int total;
     // calling function from library
     total = addition (10, 20); 
     printf ("Total = %d \n", total);

Output: Total = 30

In the next article, I am going to discuss Storage Classes in C with Examples. Here, in this article, I try to explain Adding user-defined functions in C Library. I hope you enjoy this Adding user-defined functions in C Library article. I would like to have your feedback. Please post your feedback, question, or comments about this article

1 thought on “Adding user defined functions in C Library”

Leave a Reply

Your email address will not be published. Required fields are marked *