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

44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?

6 Answers   Amdocs,


What kind of structure is a house?

0 Answers  


What are the advantages and disadvantages of c language?

0 Answers  


c program to compute Income tax and Net Salary for its employees. The company offers tax relief of Kshs. 650 for single employees and Kshs. 1,100 for married employees. The relief will be deducted from the Gross salary, to give the taxable income. This will be computed at the following rates: [10mks] Taxable Income Rate (%) <5000 0 5000-19999 6 20000-36999 9 37000 and above 16

1 Answers  


write a c program to accept a given integer value and print its value in words

4 Answers   Vernalis, Vernalis Systems,






What is restrict keyword in c?

0 Answers  


What is the meaning of this decleration? unsigned char (*pArray[10][10]); please reply.

1 Answers  


How can you avoid including a header more than once?

0 Answers  


1.what are local and global variables? 2.what is the scope of static variables? 3.what is the difference between static and global variables? 4.what are volatile variables? 5.what is the use of 'auto' keyword? 6.how do we make a global variable accessible across files? Explain the extern keyword? 7.what is a function prototype? 8.what does keyword 'extern' mean in a function declaration?

0 Answers  


If an old women's age is the same as her three grand daughters i,mean the number of days old child=the no of weeks old child=no of months old child .The total yrs of all these ppl is 114 yrs...then how old is the old woman? the yr has 365 days..and 30 days each month.

1 Answers   TCS,


What is the difference between typedef struct and struct?

0 Answers  


int *p=20; if u print like dis printf("%d",p); o\p:- 20; how is it possible? plz give me the explanation.

15 Answers   Global Edge,


Categories