Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

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;
}

Try it Yourself »

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

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;
}

Try it Yourself »

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;
}

Try it Yourself »

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;
}

Try it Yourself »

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


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.