C के Callback Functions
In this page:
void function_name(return_type (*callback)(parameter_types)) {
callback(arguments);
}
function_name(callback_function);
Callback क्या है?
एक callback एक function है जो आप एक argument के तौर पर किसी दूसरे function को सौंपते हैं, ताकि receiving function अपने execution के दौरान सही moment पर इसे वापस call कर सके, caller के इसे exactly control करने की बजाय कि यह कब चलता है।
Control का यह inversion library code को generic रहने देता है जबकि आपकी specific logic सही point पर चलती है।
उदाहरण: What is a Callback?
// Include standard input/output (printf, scanf)
#include <stdio.h>
void sayHello() {
// Print formatted text to the screen
printf("Hello");
}
void runCallback(void (*callback)()) {
callback();
}
// Program execution starts in main()
int main() {
runCallback(sayHello);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Simple Callback बनाना
एक callback accept करने के लिए, एक function अपने किसी parameter को एक function pointer type declare करता है, फिर जहाँ भी customizable behavior होना चाहिए वहाँ उस parameter को एक normal function call की तरह invoke करता है।
यह fixed control-flow logic (जैसे एक list पर iterate करना) को variable part से अलग करता है (हर element के साथ क्या करना है), जो caller supply करता है।
उदाहरण: Creating a Simple Callback
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet() {
// Print formatted text to the screen
printf("Hi there");
}
void doAction(void (*action)()) {
action();
}
// Program execution starts in main()
int main() {
doAction(greet);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Parameters वाले Callbacks
एक callback का अपना signature parameters शामिल कर सकता है, calling function को हर बार यह invoke होने पर computed values — जैसे current array element या एक index — सीधे आपकी custom logic में pass करने देते हुए।
यह एक generic driver function को कई अलग callbacks support करने देता है जिनमें हर एक को अपना काम करने के लिए अलग context चाहिए।
उदाहरण: Callbacks with Parameters
// Include standard input/output (printf, scanf)
#include <stdio.h>
void printSquare(int x) {
// Print formatted text to the screen
printf("%d", x * x);
}
void process(int value, void (*callback)(int)) {
callback(value);
}
// Program execution starts in main()
int main() {
process(5, printSquare);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Callbacks से Arrays Filter करना
एक generic array-processing function में एक callback pass करना उस same function को सिर्फ इसके आधार पर elements filter, transform, या inspect करने देता है कि कौन सा callback supply किया गया, हर use case के लिए loop logic duplicate किए बिना।
यह वही pattern है जो एक custom filterArray(arr, n, predicateFn) जैसे functions के पीछे है।
उदाहरण: Filtering Arrays with Callbacks
// Include standard input/output (printf, scanf)
#include <stdio.h>
int isEven(int n) {
// Send n % 2 == 0 back to the caller
return n % 2 == 0;
}
void filterPrint(int arr[], int size, int (*test)(int)) {
// Loop: repeat while the condition holds
for (int i = 0; i < size; i++) {
// Check whether test(arr[i])
if (test(arr[i])) {
// Print formatted text to the screen
printf("%d ", arr[i]);
}
}
}
// Program execution starts in main()
int main() {
int nums[4] = {1, 2, 3, 4};
filterPrint(nums, 4, isEven);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Callbacks के Benefits और Use Cases
Callbacks ही qsort() जैसे library functions को genuinely general-purpose बनाते हैं: qsort() को खुद नहीं पता कि आपका specific data कैसे compare करना है, इसलिए यह वह decision पूरी तरह आपके pass किए comparison callback को सौंप देता है।
यह एक single sorting implementation को integers, strings, structs, या किसी भी चीज़ पर सही काम करने देता है जिसके लिए आप एक comparator define करते हैं।
उदाहरण: Benefits and Use Cases of Callbacks
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
int compareInts(const void *a, const void *b) {
// Send (*(int*)a - *(int*)b) back to the caller
return (*(int*)a - *(int*)b);
}
// Program execution starts in main()
int main() {
int arr[3] = {3, 1, 2};
qsort(arr, 3, sizeof(int), compareInts);
// Print formatted text to the screen
printf("%d %d %d", arr[0], arr[1], arr[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक ऐसा callback pass करना जिसका signature receiving function की उम्मीद से match न करे।
- पहले check किए बिना एक
NULLcallback pointer को call करना। - इसे pass करते समय function नाम को parentheses के साथ लिखना, जो इसे pass करने की बजाय call कर देता है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions