#include <stdio.h>
// 回调函数类型,接受一个整数参数并返回void
typedef void (*Callback)(int);
// 函数:执行操作并调用回调函数
void performOperation(int x, Callback callback) {
printf("Performing operation with input: %d\n", x);
// 调用回调函数
callback(x);
}
// 回调函数:打印输入的值
void printValue(int value) {
printf("Value is: %d\n", value);
}
// 回调函数:打印输入值的平方
void printSquare(int value) {
printf("Square of value is: %d\n", value * value);
}
int main() {
// 使用回调函数printValue
performOperation(5, printValue);
// 使用回调函数printSquare
performOperation(7, printSquare);
return 0;
}
回调函数的简单用法
最新推荐文章于 2024-06-06 15:42:51 发布