当前位置 : 主页 > 编程语言 > java >

C语言 | 回调函数实践实例

来源:互联网 收集:自由互联 发布时间:2023-03-22
C语言 | 回调函数实践实例 实例1: /******************************************************************* * File Name: callback.c * Create Time: Wed Sep 8 15:37:41 2021 **************************************************************

C语言 | 回调函数实践实例

实例1:

/******************************************************************* * > File Name: callback.c * > Create Time: Wed Sep 8 15:37:41 2021 ******************************************************************/#include <stdio.h>#include <string.h>double add(int a, int b){ printf("%s\n", __func__); return (double)(a+b);}double sub(int a, int b){ printf("%s\n", __func__); return (double)(a - b);}double mul(int a, int b){ printf("%s\n", __func__); return (double)(a*b);}double div(int a, int b){ printf("%s\n", __func__); return ((double)a/(double)b);}typedef struct _Calculator{ char objname[24]; double (*pAdd)(int, int); double (*pSub)(int, int); double (*pMul)(int, int); double (*pDiv)(int, int);}CALCULATOR, *pCALCULATOR;int main(int argc, char* argv[]){ printf("a + b = %f\n", add(100, 200)); printf("a - b = %f\n", sub(100, 200)); printf("a * b = %f\n", mul(100, 200)); printf("a / b = %f\n", div(100, 200)); double (*pFunc)(int, int); pFunc = add; pFunc(10,10); CALCULATOR obj; strcpy(obj.objname, "calculator"); obj.pAdd = add; obj.pSub = sub; obj.pMul = mul; obj.pDiv = div; printf("============================>\n"); obj.pAdd(10,10); obj.pSub(10,10); obj.pMul(10,10); obj.pDiv(10,10); return 0;}

编译、运行:

lanfeiy@yelanfei ~/workspace/test$ make -f c.mk callbackgcc -o callback callback.c -g -Walllanfeiy@yelanfei ~/workspace/test$ ./callback.exeadda + b = 300.000000suba - b = -100.000000mula * b = 20000.000000diva / b = 0.500000add============================>addsubmuldiv

​​C语言 | C语言中的四种特殊函数​​

​​C语言 | 编程技巧-跳转表​​

上一篇:JavaWeb中URL匹配规则详解
下一篇:没有了
网友评论