callback

please brief me the meaning of callback in C with some example
A callback function is a function that you provide to somebody so that they can call it.

An example is the famous WndProc function that we often make when programming in windows 32. We need to tell windows which function will handle the messages sent by a window. We do this by "registering" our function with Windows. That function is then a callback function.
Customizable behaviour.

man atexit:
NAME
       atexit  -  register a function to be called at normal process
       termination

SYNOPSIS
       #include <stdlib.h>

       int atexit(void (*function)(void));

DESCRIPTION
       The atexit() function registers  the  given  function  to  be
       called  at  normal process termination, either via exit(3) or
       via return from the program’s main().   Functions  so  regis-
       tered  are called in the reverse order of their registration;
       no arguments are passed.

       The same function may be registered  multiple  times:  it  is
       called once for each registration.

The standard library has an array of function pointers. The atexit function can be used to add pointers to that array. When the execution of a program ends, every function in the array will be called.

This is an optional feature. It makes possible for you to execute a custom procedure written by you at that event.


A C++ example of a callback: http://www.cplusplus.com/reference/numeric/accumulate/
The accumulate() iterates over elements of an array. One has to provide a pointer to a function, if one calls the "custom" version. The accumulate() will call that function with each element in the array.


In both examples the libraries provide "boilerplate" code, but leave the option for the application to supply custom code for specific steps. The library authors could not possibly know the needs of an application, nor can they provide all possible enumerations. The library calls your function. The library calls "back".
Topic archived. No new replies allowed.