我的C代码有什么问题?

跌落
float x = 4.5;
main()
{
    float y,float f(); 
    x*=2.0;
    y=f(x);
    printf("\n%f%f",x,y);
}

float f (float a)

{
    a+=1.3;
    x-=4.5;
    return(a+x);
}

该程序摘自Yashwant kanetkar的《让我们C》一书。它说的输出是4.500000 5.800000。我出错了。

亨里克
//global variable x, can be accessed from anywhere
float x = 4.5;

//inform the compiler, that a function that is defined later, but it exists in the code, and to use it before it's defined, we need to know how it looks like:
float f(float a);

//I need to specify what my main function returns, typically "int"
int main()
{
    //local variable for saving the result in:
    float y;

    //multiply x, with 2, and save the result in x:
    x*=2.0;

    //run the function:
    y=f(x);

    //show the result:
    printf("\n%f%f",x,y);

    //return a value to let the OS know how the program ended (0 is success)
    return 0;
}

//define the function
float f (float a)
{ 
    //whatever a is provided, add 1.3 to it before doing anything else
    a+=1.3;
    //subtract 4.5 from the global variable, before doing anything else:
    x-=4.5;
    //return the sum of a and x, as the result of this function.
    return(a+x);
}

这里的数学是:

x = 4.5
x = (x * 2) = 9
a = (x = 9) = 9
a = (a + 1.3) = 10.3
x = (x - 4.5) = 4.5
y = a + x = (4.5 + 10.3) = 14.8

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章