what is the use of call back function in c?tell me with
example
Answer Posted / abdur rab
The caller and callee are decoupled.
The caller doesn't know who the callee is; all it knows is
that there is a callee with a certain prototype and
probably some restriction (for instance, the returned value
can be int, but certain values have certain meanings).
This would be useful during the creation of libraries where
in you do not want the logic to be embedded in the library.
hele let us consider a function do_action exists in the
library. It takes three parameters (int, int, and a
function)
The do_action does not know what the passed function does.
#include <stdio.h>
int add ( int x, int y )
{
return ( x + y );
}
int sub ( int x, int y )
{
return ( x - y );
}
int mul ( int x, int y )
{
return ( x * y );
}
int div ( int x, int y )
{
return ( x / y );
}
int do_action ( int x, int y, int (*callback_function)
(int, int) )
{
return ( (*callback_function) ( x, y ) );
}
int main ( int argc, char* argv [] )
{
int x = 10;
int y = 2;
printf ("\nAdd %d", do_action ( x, y, &add ) );
printf ("\nSub %d", do_action ( x, y, &sub ) );
printf ("\nMul %d", do_action ( x, y, &mul ) );
printf ("\nDiv %d", do_action ( x, y, &div ) );
return ( 0 );
}
This is just an example. the usage of callback is more than
this
Is This Answer Correct ? | 48 Yes | 3 No |
Post New Answer View All Answers
What do you mean by c?
Why does the call char scanf work?
What is the difference between the = symbol and == symbol?
What is the size of structure pointer in c?
Explain indirection?
What is string concatenation in c?
What is structure in c explain with example?
What is the difference between text and binary modes?
Write a program to use switch statement.
Is null valid for pointers to functions?
Does sprintf put null character?
What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }
What are the advantages of union?
What is the difference between void main and main in c?
Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?