← Back to C Course | Chapter 12: Advanced Topics | Lesson 7 of 20

C के Callback Functions

एक callback एक function है जो आप किसी दूसरे function को सौंपते हैं ताकि वह बाद में आपका call कर सके, दोबारा call होने के लिए अपना phone number छोड़ने जैसा। यह एक code के piece को दूसरे को customize करने देता है।
Syntax
c
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?

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

एक 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

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

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

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

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

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

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक ऐसा callback pass करना जिसका signature receiving function की उम्मीद से match न करे।
  2. पहले check किए बिना एक NULL callback pointer को call करना।
  3. इसे pass करते समय function नाम को parentheses के साथ लिखना, जो इसे pass करने की बजाय call कर देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.