Here is the code i was trying.I could not comprehend the output i got.Why is output some garbage value here and not 10?
#include<stdio.h>
#include<string.h>
void f(int);
void (*foo)(float)=f;
int main()
{
foo(10);
return 0;
}
void f(int i)
{
printf("%d\n",i);
}
Output is some garbage value.
This is undefined behaviour.
You’re calling
f, which is a function taking anint, as if it were a function taking afloat. The actual behaviour will depend on how the calling conventions of your platform work – for example, ifintandfloatparameters are passed in the same register or the same stack slot the result will be that of aliasing the floating-point bit pattern representing10.0fas anint. If on the other handintandfloatare passed differently the result will be whatever garbage was in the appropriate register or stack slot.Also, because this is undefined behaviour the compiler is at liberty to do whatever it likes, and if the optimiser is enabled it may well do.