Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use callback function in C language

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use the callback function of C language". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn the article "how to use the callback function of C language".

What is a callback function

Because in the program, we have a lot of library functions, we also have a lot of upper functions, in order to increase the flexibility of the program, we pass some function pointers as parameters to the function.

To put it roughly, it means to take one function as the function parameter of another function.

To call the callback function, we need an intermediate function to transition.

The argument to this intermediate function is a function pointer.

Second, a simple callback function

Let's write a simple example to help us understand:

# include / * callback function 1*/void callBack1 (void) {printf ("this is callback1\ n");} / * callback function 2*/void callBack2 (void) {printf ("this is callback2\ n");} / * intermediate function * / / * argument is a function pointer * / void calc (void (* func) ()) {func ();} int main () {calc (callBack1) Printf ("*\ n"); calc (callBack2); return 0;}

Analyze the use of this simple callback function.

The function starts from the main function, and we pass the callback function (callBack) to the intermediate function (calc) as an argument, which is accepted by the function pointer and executes different results by passing in different callback functions ((callBack1), (callBack2)).

Third, callback function with parameters

The above program simply explains the callback function without parameters, so we can also follow the callback function with parameters.

# include / * callback function 1*/int callBack1 (int aline int b) {printf ("this multiplication:\ n"); return a * b;} / * callback function 2*/int callBack2 (int a pencil int b) {printf ("this addition:\ n"); return a + b } / * Intermediate function * / / * the argument is a function pointer * / int calc (int a _ line int bline int (* func) ()) {func ();} int main () {int p; int q; p = calc (20line 20, callBack1); printf ("% d\ n", p); printf ("*\ n") Q = calc (20,20, callBack2); printf ("% d\ n", Q); return 0;}

In our work, our callback function is certainly not so simple, but the function of the callback function is not to change the underlying program, but to get different data by getting the underlying data. In this way, there will be a lot of changes in our upper layer, but our underlying program will not move.

The above is all the contents of the article "how to use the callback function in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report