Table of contents
1.
Introduction
2.
Example
3.
Frequently Asked Questions
3.1.
What is C?
3.2.
What is a callback function?
3.3.
How to create callback functions in C++?
3.4.
What are header files?
3.5.
What is the stdio.h header file?
4.
Conclusion
Last Updated: Apr 30, 2024
Easy

Callback Function in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C is a general-purpose programming language. C is commonly used in operating systems, device driver code, and protocol stacks. The C programming language is not to be confused with the C++ programming language, which is an extension of the C programming language.

A callback function is any function that receives the reference of another function as the argument to call the function. We can call a callback function using the function pointers in the C programming language. We can also use the STL(Standard Template Library) Functors in C++ to create callback functions.

You can also read about C dynamic array, C Static Function

Example

Let us look at examples of the callback functions in the C programming language,

#include<stdio.h>
void passFunction()
{
printf("Function to be passed\n");
}


void callBackFunction(void (*ptr)())
{
(*ptr) ();
}


int main()
{
void (*ptr)() = &passFunction;
callBackFunction(ptr);
return 0;
}
You can also try this code with Online C Compiler
Run Code

In the above example, we pass the passFunction as a reference to the callBackFunction and print the value using the callBackFunction.

Also Read About, Sum of Digits in C, Tribonacci Series and  Short int in C Programming

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

What is C?

C is a general-purpose programming language. C is commonly used in operating systems, device driver code, and protocol stacks.

What is a callback function?

A callback function is any function that receives the reference of another function as the argument to call the function. We can call a callback function using the function pointers in the C programming language.

How to create callback functions in C++?

We can also use the STL(Standard Template Library) Functors in C++ to create callback functions.

What are header files?

Header files are a collection of pre-defined functions that are used to make the developer's programming experience more efficient and faster. Developers can import various header files using the include keyword in the C and C++ programming languages.

What is the stdio.h header file?

The stdio.h header file includes all the essential information regarding the input and the output related functions in the C and C++ programming languages.

Conclusion

This blog covered all the necessary points about the callback function in C programming. We further looked at a program to understand the working of the callback functions. Do check out our blogs on object-oriented programming and data structures

Don’t stop here. Check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass