0
0
CHow-ToBeginner · 3 min read

How to Use Callback Function in C: Simple Guide with Example

In C, a callback function is a function passed as an argument to another function to be called later. You use function pointers to pass and invoke callbacks, allowing flexible and reusable code.
📐

Syntax

A callback function in C is implemented using a function pointer. You declare a function pointer parameter in a function, then pass the address of a function matching that pointer's signature.

  • return_type (*pointer_name)(parameter_types): declares a function pointer.
  • function_name: the callback function you want to call.
  • function_call(pointer_name(...)): calls the callback inside another function.
c
#include <stdio.h>

void callbackExample(void (*callback)(int)) {
    callback(5);  // Call the passed function with argument 5
}

void myCallback(int num) {
    // This function matches the pointer signature
    printf("Callback called with %d\n", num);
}
💻

Example

This example shows how to define a callback function and pass it to another function that calls it.

c
#include <stdio.h>

// Function that takes a callback
void processNumber(int num, void (*callback)(int)) {
    printf("Processing number: %d\n", num);
    callback(num);  // Call the callback with the number
}

// Callback function
void printSquare(int n) {
    printf("Square is: %d\n", n * n);
}

int main() {
    processNumber(4, printSquare);  // Pass printSquare as callback
    return 0;
}
Output
Processing number: 4 Square is: 16
⚠️

Common Pitfalls

Common mistakes when using callbacks in C include:

  • Passing a function with the wrong signature (parameters or return type).
  • Forgetting to use the * when declaring function pointers.
  • Calling the callback without checking if it is NULL, which can cause crashes.
  • Confusing function pointers with regular pointers.

Always ensure the callback matches the expected signature and check for NULL if callbacks are optional.

c
/* Wrong: callback signature mismatch */
void wrongCallback(float x) {
    printf("Wrong callback\n");
}

/* Correct: matching signature */
void correctCallback(int x) {
    printf("Correct callback with %d\n", x);
}

/* Usage */
void callFunc(void (*cb)(int)) {
    if (cb != NULL) {
        cb(10);
    }
}
📊

Quick Reference

ConceptDescriptionExample
Function Pointer DeclarationDeclare a pointer to a function with specific parameters and return typevoid (*cb)(int)
Passing CallbackPass function name without parentheses to another functionprocessNumber(5, printSquare)
Calling CallbackInvoke callback inside function using pointer syntaxcallback(num)
Check NULLAlways check if callback is NULL before callingif (callback) callback(num);

Key Takeaways

Use function pointers to pass callback functions in C.
Ensure the callback function signature matches the pointer declaration.
Always check if the callback pointer is not NULL before calling it.
Callbacks allow flexible and reusable code by deferring function calls.
Passing the function name without parentheses passes the pointer.