Home  >  Article  >  Backend Development  >  callback function in C

callback function in C

WBOY
WBOYforward
2023-09-01 23:25:10928browse

callback function in C

A callback function is basically any executable code passed as a parameter to other code that is expected to be called or executed at a specific time. We can define it in other words as follows: If a reference to a function is passed as a parameter to another function to call, it is called a callback function.

In C language, we must use function pointers to call callback functions. The following code shows how the callback function performs its task.

Sample code

#include<stdio.h>
void my_function() {
   printf("This is a normal function.");
}
void my_callback_function(void (*ptr)()) {
   printf("This is callback function.</p><p>");
   (*ptr)();   //calling the callback function
}
main() {
   void (*ptr)() = &my_function;
   my_callback_function(ptr);
}

Output

This is callback function.
This is a normal function.

The above is the detailed content of callback function in C. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete