- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Callback Function in C
Callback functions are extremely versatile, particularly in event-driven programming. When a specific event is triggered, a callback function mapped to it is executed in response to these events. This is typically used in GUI applications, an action like a button click can initiate a series of predefined actions.
Callback Function
The callback function is basically any executable code that is passed as an argument to other code, that is expected to call back or execute the argument at a given time. We can define it in other words like this: If the reference of a function is passed to another function argument for calling, then it is called a callback function.
The mechanism of callbacks depends on function pointers. A function pointer is a variable that stores the memory address of a function.
Here is a simple hello() function in C −
void hello(){ printf("Hello World."); }
We declare a pointer to this function as follows −
void (*ptr)() = &hello;
We can now call the function with the help of this function pointer, (*ptr)();
Example of Callback Function in C
In this example, the hello() function is defined as an argument to myfunction().
#include <stdio.h> void hello(){ printf("Hello World\n"); } void callback(void (*ptr)()){ printf("Calling a function with its pointer\n"); (*ptr)(); // calling the callback function } main(){ void (*ptr)() = hello; callback(ptr); }
Output
Calling a function with its pointer Hello World
Callback Function With Arguments
In the example given below, we have also declared two functions with identical prototypes − square() and root().
int square(int val){ return val*val; } int root(int val){ return pow(val, 0.5); }
The callback function is defined to receive an argument as well as a function pointer with an integer argument that matches with the above functions.
int callback(int a, int (*ptr)(int)){ int ret = (*ptr)(a); return ret; }
In the main() function, we place a call to the callback by passing an integer and the name of the function (square / root) which becomes the function pointer in callback’s definition.
Example of Callback Function With Arguments
The complete code is as follows −
#include <stdio.h> #include <math.h> int callback(int a, int (*print_callback)(int)); int square(int value); int root (int value); int main(){ int x = 4; printf("Square of x: %d is %d\n", x, callback(x, square)); printf("Square root of x: %d is %d\n", x, callback(x, root)); return 0; } int callback(int a, int (*ptr)(int)){ int ret = (*ptr)(a); return ret; } int square(int val){ return val*val; } int root(int val){ return pow(val, 0.5); }
Output
Square of x: 4 is 16 Square root of x: 4 is 2
Types of Callbacks in C
There are two types of callbacks −
Synchronous Callback
A callback is synchronous when it is given to another function, which executes it as part of its process. The calling function waits for the callback to complete before proceeding. This is useful when you need immediate results or want to ensure a task is finished before moving on.
Asynchronous Callback
In this case, the calling function triggers the callback but doesn’t wait for it to finish. Instead, it continues its execution. It results in non-blocking operations. It’s commonly used in event-driven programming.
Generic callback functions help developers write C programs that are versatile and better adaptable.
In this chapter, we explained how you can use function pointers so that we can enhance the flexibility of our C programs. Additionally, we showed how you can create generic callback functions that are not limited to a specific function pointer type.