C Callback Functions
Callback Function
A callback function is a function that is passed as an argument to another function.
The receiving function can then call it back (run it) whenever it needs to.
This is a powerful way to make your code flexible and reusable - you can decide which function should run, without changing the main logic.
In C, callback functions are usually implemented using function pointers.
Simple Callback Example
Here's a basic example that shows how one function can "call back" another:
Example
Passing a function as an argument to another function:
void sayHello() {
printf("Hello from the callback!\n");
}
void runCallback(void (*callback)()) {
printf("Before calling the callback...\n");
callback();
printf("After calling the callback.\n");
}
int main() {
runCallback(sayHello);
return 0;
}
In this example:
sayHello()is a normal function that prints a message.runCallback()receives a function pointer as a parameter.- When
runCallback(sayHello)runs, it callssayHello()inside itself.
Note: The word "callback" comes from the fact that one function "calls back" another that was given to it earlier.
Callback with Parameters
You can also pass functions that take parameters - just make sure the function pointer type matches:
Example
Passing a function with parameters as a callback:
void addNumbers(int a, int b) {
printf("The sum is: %d\n", a + b);
}
void calculate(void (*callback)(int, int), int x, int y) {
callback(x, y);
}
int main() {
calculate(addNumbers, 5, 3);
return 0;
}
Here, calculate() receives a function and two numbers,
then calls the function (in this case, addNumbers()) with those numbers as arguments.
Multiple Callbacks
You can use callbacks to let a function behave differently depending on which function you pass in. This is common in sorting, filtering, and event-handling code.
Example
Using different callback functions to perform different actions:
void greetMorning() { printf("Good morning!\n"); }
void greetEvening() { printf("Good evening!\n"); }
void greet(void (*callback)()) {
callback();
}
int main() {
greet(greetMorning);
greet(greetEvening);
return 0;
}
Depending on which function is passed, the program produces different output - without changing greet() itself.
Real-World Example: Using Callbacks in qsort()
Many C standard library functions use callbacks.
For example, the qsort() function in <stdlib.h> uses a callback to compare elements while sorting.
You provide the comparison function, and qsort() calls it as needed.
This will sort the elements:
Example
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int numbers[] = { 5, 2, 9, 1, 7 };
int size = sizeof(numbers) / sizeof(numbers[0]);
qsort(numbers, size, sizeof(int), compare);
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Here, compare() is the callback function used by qsort() to decide how to order the numbers.
Summary
- A callback function is a function passed as an argument to another function.
- It allows one function to call another without knowing its name in advance.
- Callbacks make code more flexible and reusable.
- They are used in the C Standard Library (like
qsort()).