In my program, I have this requirement where I have to pass a float value back to the main function.
The code for the first approach is:
#include <stdio.h>
void main()
{
int a,b;
a=1;
b=2;
division(a,b);
}
int division(int a, int b)
{
float res;
res=((float) a)/b;
printf("%f",res);
}
The code for the second approach (where I pass the float value back to the main function) is this:
#include <stdio.h>
void main()
{
int a,b;
a=1;
b=2;
float res;
res=division(a,b);
printf("%f",res);
}
int division(int a, int b)
{
float res;
res=((float) a)/b;
return res;
}
For the first approach, the output is 0.5
, as expected. However, when I pass the result float
(as shown in the next program), I am getting an output as 0
. Why does this happen ? Is there any work-around for this ? I am relatively new to C (trying something apart from Java), so I am not very familiar with it's rules. When I modified int division
to include a printf
statement for variable res
, like
int division(int a, int b)
{
float res;
res=((float) a)/b;
printf("Variable before passing %f ",res);
return res;
}
I am getting the output as Variable before passing 0.500000 0.000000
, which lead me to believe that the problem is with the passing of parameters back to the main function.