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

wht is the difference between KPO and BPO ?

2 Answers   Accenture, BPO, HCK, HCL, Infosys,


The C language terminator is a.semicolon b.colon c.period d.exclamation mark

6 Answers   TCS,


How do we print only part of a string in c?

0 Answers  


How can a program be made to print the name of a source file where an error occurs?

0 Answers  


please help me..... please codes and flowchart plz turbo c lang po yan.....please asap response... 3. Make an astrology program. The user types in his or her birthday (month, day, and year as integer), and the program responds with the user’s zodiac sign, horoscope, and other information related to it. If the user’s birth year falls into a leap year, your program should display an appropriate message for it. NOTES: Conditional Statements: it should be with graphics

0 Answers  






write a pgm to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1

3 Answers  


What are nested functions in c?

0 Answers  


input may any number except 1,output will always 1.. conditions only one variable should be declare,don't use operators,expressions,array,structure

4 Answers   IBM,


Why array starts with index 0

2 Answers  


What are c preprocessors?

0 Answers  


write a fuction for accepting and replacing lowercase letter to'Z' with out using inline function.

5 Answers   Temenos,


What is a file descriptor in c?

0 Answers  


Categories