what is the use of call back function in c?tell me with
example

Answers were Sorted based on User's Feedback



what is the use of call back function in c?tell me with example..

Answer / 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

what is the use of call back function in c?tell me with example..

Answer / kapil sharma

callback function as the name suggest is basically a
function which gets called at the run time. it is a
reference to the executable code or a part of executable
code which is passed as an argument to another function.
for e.g if we want to forcefully terminate a program but if
there is some essential data which will get lost if program
gets terminated instantly.in that case writing a callback is
always a good deal.
---before writing a callback function you must have
knowledge of function pointers.

Is This Answer Correct ?    7 Yes 6 No

Post New Answer

More C Interview Questions

How do you view the path?

0 Answers  


What is the output from this program? #include <stdio.h> void do_something(int *thisp, int that) { int the_other; the_other = 5; that = 2 + the_other; *thisp = the_other * that; } int main(void) { int first, second; first = 1; second = 2; do_something(&second, first); printf("%4d%4d\n", first, second); return 0; }

3 Answers  


I came across some code that puts a (void) cast before each call to printf. Why?

0 Answers  


What is the maximum length of an identifier?

0 Answers  


Why is c platform dependent?

0 Answers  






write a c programs to do multiplication of two numbers with out using arithmatic operator ??????????

7 Answers   Infosys, TCS,


Define a structure to store the record of library. The record must consist of at least following fields: Title, Author, Edition, Price, Publisher, and Category. -Define functions authorSearch ( ), TitleSearch ( ) and CategorySearch ( ) to search a book with respect to author, title and category. [There can be more than one book, written by one author, in one category]

2 Answers  


What will be printed as the result of the operation below: #include<..> int x; int modifyvalue() { return(x+=10); } int changevalue(int x) { return(x+=1); } void main() { int x=10; x++; changevalue(x); x++; modifyvalue(); printf("First output:%d\n",x); x++; changevalue(x); printf("Second output:%d\n",x); modifyvalue(); printf("Third output:%d\n",x); }

2 Answers  


Tell me about low level programming languages.

0 Answers   Amdocs,


What is the time and space complexities of merge sort and when is it preferred over quick sort?

0 Answers   Amazon,


Is multithreading possible in c?

0 Answers  


What is call by reference in functions?

1 Answers  


Categories