【问题标题】:What's wrong with my C code?我的 C 代码有什么问题?
【发布时间】:2015-06-26 19:18:58
【问题描述】:
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 的书:Let us C。它说的输出是 4.500000 5.800000。我得到错误。

【问题讨论】:

  • 你得到什么错误?
  • 程序甚至无法编译!
  • 尝试改用float y, f(float);
  • [1]。当您询问编译错误问题时,请始终包含确切的错误消息(无需编辑)。 [2]。不要从 Yashwant Kanetkar 阅读,它已经过时了。参考一些good book

标签: c types


【解决方案1】:
//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

【讨论】:

  • 通知编译器很重要,我忘了。您的解释清楚了,谢谢。
  • 我认为这行:y = a + x = (4.5 + 10.3) = 15.8 应该是y = a + x = (4.5 + 10.3) = 14.8
【解决方案2】:

第 1 点:

main() 中删除float f();。在main() 之外声明float f();。因此,它将服务于前向声明的目的。

第 2 点。

删除, 并在float y 之后添加;

第 3 点。

main() 应该是int main(void)

注意:考虑在main() 的末尾添加一个显式的return 语句。虽然不是强制性的,但被认为是一种好的做法。

【讨论】:

    【解决方案3】:

    当你声明f():

    float y,float f();
    

    您没有使用原型。所以当调用f() 时:

    y=f(x);
    

    编译器将float 参数x 提升为double 类型。

    要解决问题,请使用完整原型声明函数:

    float f(float a);
    

    【讨论】:

      【解决方案4】:

      无需在 main.js 中声明变量之类的函数。 2. 另外,main() 应该是int main()。 3. 在使用之前定义/声明你的函数。

       #include <stdio.h>
      float x = 4.5;
      
      float f (float a)
      
      {
      a+=1.3;
      x-=4.5;
      return(a+x);
      }
      int main(void) {
      // your code goes here
      float y;
      
      x*=2.0;
      y=f(x);
      printf("\n%f%f",x,y);
      return 0;
      }
      

      Ideone 链接:http://ideone.com/W7V1Qr

      【讨论】:

      • 但是如果我先声明 main() 然后再声明另一个函数,为什么会显示错误:ag.c:13:7: error: conflicting types for 'f' float f (float a) ^ ag.c:9:7: 注意:'f' 的先前隐式声明在这里 y=f(x);
      • 那是因为每当你引用一个函数时,编译器应该已经知道那个函数了。
      • 是的,基础知识!只是忘记了。不过现在明白了。谢谢。
      【解决方案5】:

      float y,float f();

      float y 后面加分号

      float y;
      

      还有float f(); //(错误的原型)。您需要将参数传递给此函数。

      无需在 main 中声明函数。您可以从 main() 函数中删除 float f();

      main 没有返回任何值

      使用

      int main()
      {
         //your code
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-28
        相关资源
        最近更新 更多