Hook() function in C
Use of hook () function in C
Introduction:
The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function code by its own code. When you call the function, then your code will run. So, hook () is a function which removes the function code.
The hook() function is a function which provides extended behaviour in the run time. A hook is a package code that gives the user an interface to write a customised program. It is a function which is also known as the call back function.
Syntax of the hook () function in C language:
Syntax means the order or arrangement in the program. The syntax is also known as the function prototype. Every function has a syntax value. The syntax of the hook() function is:
Int hookFunction();
The hook() function uses to remove the function code by its own code.
Header file of the hook () function in C language :
Every program contains some header files. For the run of any program, you must need a header file. Without a header file, you can't run any programs. So, we must produce the header file in every program. It is a file with an extension.
The primary use of the header file is to propagate the declaration of code files. The hook () function uses as a library function. The header file of the hook () function in the C language is <stdio.h>.
It writes in the program as #include <stdio.h>.
Example 1 –
Here we given a example of the hook () function using C language:
#include <stdio.h>
int hookTargetFunction ()
{
printf ("Calling the original function!\ n");
return 10,
}
int main ()
{
printf (“The number is: %d\n”, hookTargetFunction());
return 0;
}
Output: We compile and run the above program.The result of this above program is:
Calling the original function!
The number is: 10
To better understand, we give another example of the hook () function.
Example 2 –
Here we given a example of the hook() function using C language:
#include <stdio.h>
void PayEmployee (Employee anEmployee, int amout)
{
company. getBankAccount () . withdraw (amount);
anEmployee . getBankAccount () . deposit (amount);
anEmployee . setPaidThisWeek (TRUE);
void *(*hookList) (Employee,int) = getPaymentHookList();
while (*hookList != NULL)
{
(*hookList) (anEmployee, amount);
++hookList;
}
}
void addPaymentHook (void (*paymentFunc) (Employee, int))
{
getPaymentHookList() . addHook (paymentFunc);
}
Output: We compile and run the above program.
In the above two programs, we have used the hook () function; by the programs, we briefly discussed how to write any C language program using the hook () function. The basic concept of the hook () function is that it can remove the function code by its own code. The hook () function is a library function.
So we <stdio.h> header file in this program. It writes in the program as #include <stdio.h>. In the above programs, we are using stdio.h header file.
The primary use of the header file is to propagate the declaration of code files. We also share the output of the above two programs.