422,727 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,727 IT Pros & Developers. It's quick & easy.

passing float value to a function convert the value to zero

P: 1
I m getting float value from one function and passing the same to another function and if i print the value im getting it as zero. for eg:
considering datatype of "f_val" is float
f_val = float_return();//here im getting value as 3.600585
func(f_val);//function call

/*function declaration*/
int func(float f_val){
printf("f_val %f\n",f_val);//here im getting print as -0.000000
return 0;
}
what can be the reason? if i declared as double it is working . so how to make it work with float
NOTE:
-----
Im using ubuntu gcc compiler 32bit
5 Days Ago #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,132
So what does float_return() do to your f_val?

I commented out that line of code, assigned 3.600585 to f_val then made the call to func(). Worked fine.
5 Days Ago #2

Expert 100+
P: 2,373
You need a function prototype for func() at the top of your source file.

Without a prototype, the compiler doesn’t know the argument to func(), when you call it, is supposed to be a float. Without a prototype the compiler has to guess. In the case of a floating point argument, the C Standard mandates that the guess be to convert the value to a double and pass that.

You must have done something to prevent this issue from arising for the call to float_return().

The rule of thumb is to _always_ declare functions before you call them. The C Standard requires compilers to accept source code that doesn’t do this. However, most compilers can be set to generate a warning for any call to an undeclared function.
1 Days ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.