🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #69

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

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 calls sayHello() 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

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

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()).

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Callback Functions – FAQs

Quick answers about learning Callback Functions in C.

This free note from CodingNow 2.0 explains Callback Functions in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including Callback Functions, is 100% free with no signup required.
With focused practice, most students grasp Callback Functions in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now