- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Callback Function
![]() Share with a Friend |
C Programming - C Callback Function
C Callback Functions
A callback function in C is a function that is passed as an argument to another function. The callback function is then invoked (called back) inside the other function, typically at some point during the execution of that function. This allows a level of customization and flexibility in how a program operates, particularly when you want a function to perform an operation, but the specific details of the operation should be provided by the caller.
What is a Callback Function?
- A callback function is a user-defined function that is passed as an argument to another function.
- The function receiving the callback function will execute it when appropriate, providing a way for the caller to dictate behavior or modify the flow of the program.
How Callback Functions Work in C
- Define a callback function: The function you will pass as an argument.
- Pass the callback function: The callback function is passed to another function that will invoke it.
- Invoke the callback: The receiving function calls the callback function at the right point in its execution.
Callback Function Example in C
Here is a simple example of a callback function in C:
C
#include <stdio.h>
// Callback function definition
void printMessage() {
printf("Hello from the callback function!\n");
}
// Function that accepts a callback function as an argument
void executeCallback(void (*callback)()) {
printf("Before calling the callback function.\n");
callback(); // Calling the callback function
printf("After calling the callback function.\n");
}
int main() {
// Passing the callback function to executeCallback
executeCallback(printMessage);
return 0;
}
Explanation:
- printMessage is the callback function that simply prints a message.
- executeCallback is a function that accepts a pointer to a function (void (*callback)()) as a parameter. It calls the callback function when needed.
- In main(), we pass the printMessage function to executeCallback, which then calls printMessage inside its body.
Output:
Before calling the callback function.
Hello from the callback function!
After calling the callback function.
Callback Functions with Parameters and Return Values
Callback functions can also accept parameters and return values. Here’s an example of a callback function with parameters and a return value:
C
#include <stdio.h>
// Callback function definition with parameters
int add(int a, int b) {
return a + b;
}
// Function that accepts a callback function with parameters
void executeOperation(int x, int y, int (*operation)(int, int)) {
int result = operation(x, y); // Calling the callback function with arguments
printf("Result of operation: %d\n", result);
}
int main() {
int num1 = 5, num2 = 3;
// Passing the add function as the callback function
executeOperation(num1, num2, add);
return 0;
}
Explanation:
- add is the callback function that adds two integers.
- executeOperation takes two integers and a function pointer (operation) that points to a function that takes two integers and returns an integer.
- Inside executeOperation, the callback function (add) is invoked with the arguments num1 and num2.
Output:
Result of operation: 8
Common Use Cases for Callback Functions
- Event Handling: Callbacks are often used in graphical user interface (GUI) programming where actions like button clicks or mouse movements need to trigger specific functions.
- Sorting Algorithms: Callback functions are used in sorting routines to compare elements.
- Asynchronous Programming: Callback functions are commonly used in asynchronous programming models to handle events or responses when they are received.
- Custom Behavior: Callback functions allow a program to execute user-defined code at specific points without modifying the core logic of the program.
Advantages of Using Callback Functions
- Separation of Concerns: The main function can delegate behavior to the callback, allowing different behaviors without changing the core logic.
- Flexibility: Callbacks provide flexibility to extend or modify behavior at runtime.
- Reusability: You can reuse a function and provide different behaviors by passing different callback functions.
- Modularity: Callback functions allow for clean separation between the logic of one function and the behavior passed in through the callback.
Conclusion
- A callback function is a mechanism where a function can accept another function as an argument and call it at an appropriate time during its execution.
- Callback functions make C programs more flexible and modular, allowing developers to customize behavior without changing the main function's logic.
