I have encounter a problem which can be summarized as follows,
#include <stdio.h>
int main()
{
float f=23.45;
printf("main: %f\n", f);
t1(f);
/* the result would be
main:23.450001
t1:2.000000 */
}
void t1(float f)
{
printf("t1: %f\n", f);
}
I know now that the weird behavior is due to missing of prototype declaration and the arguments are thus promoted,(float->double?),still i cannot figure out why the result is 2.000000,so can anyone give a more detailed explanation? I am using ubuntu10.04 gcc4.4.3
The behavior you are observing is specific to stack-based parameter passing. For people who compile 64-bit x86 code by default and are unable to reproduce it, you can try with “gcc -m32” instead of just “gcc”.
With stack-based parameter passing,
t1()read 32 bits from the stack, and these 32 bits happen to form the floating-point value2.0.At the call site, because
t1did not have a prototype, the argumentfwas promoted todoubleand it was adoublethat was written on the stack (C99 6.5.2.2:6 “If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions”).There is no reason for
t1to recover the intended value from the stack since it does not read it properly with the same type and width it was written.