search
HomeCommon ProblemHow to implement callback function in C language

How to implement callback function in C language

Apr 16, 2019 pm 02:12 PM
c languageCallback

The callback function is to implement different functions through a unified interface. The callback function in C language is to call different callback functions in the code according to the different parameters passed

The callback function is a program A function that cannot be called explicitly by members; the callback function is called by passing the address of the callback function to the caller. The use of callback functions is necessary. When we want to implement different content through a unified interface, callback functions are very suitable.

How to implement callback function in C language

【Recommended tutorial: C language tutorial

For example, we wrote different display functions for several different devices:

void TVshow(); 
void ComputerShow(); 
void NoteBookShow()...等等。

This is because we want to use a unified display function, and we can use the lost function at this time.

void show(void (*ptr)());

When used, different callback functions are called according to the parameters passed in.

Different programming languages ​​may have different syntax. Here is an example of a callback function in C language. One callback function does not take parameters, and the other callback function takes parameters.

#include <stdlib.h>
#include <stdio.h>
int Test1()
{
    int i;
    for(i=0; i<30; i++)
    {
        printf("The %d th charactor is: %c\n", i, (char)(&#39;a&#39; + i%26));
    }
    return 0;
}
int Test2(int num)
{
    int i;
    for (i=0; i<num; i++)
    {
        printf("The %d th charactor is: %c\n", i, (char)(&#39;a&#39; + i%26));
    }
    return 0;
}
void Caller1( void (*ptr)() )//指向函数的指针作函数参数
{
    (* ptr)();
}
void Caller2(int n, int (*ptr)())//指向函数的指针作函数参数,这里第一个参数是为指向函数的指针服务的,
{
    //不能写成void Caller2(int (*ptr)(int n)),这样的定义语法错误。
    (* ptr)(n);
}
int main()
{
    printf("************************\n");
    Caller1(Test1);     //相当于调用Test1();
    printf("&&&&&&************************\n");
    Caller2(30, Test2); //相当于调用Test2(30);
    return 0;
}

The rendering is as follows

How to implement callback function in C language

The above call is implemented by passing the address of the callback function to the caller, but it should be noted that the callback function with parameters usage.

To implement callbacks, you must first define a function pointer. The definition of function pointers is briefly mentioned here.

For example:

int (*ptr)();

Here ptr is a function pointer, in which the brackets of (*ptr) cannot be omitted,

because the priority of brackets is higher than that of asterisks, so It becomes a function declaration with an integer return type.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of How to implement callback function in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor