【问题标题】:How to output the value returned by a function that is a float in C如何输出由C中的浮点函数返回的值
【发布时间】:2017-04-30 02:13:50
【问题描述】:

问题是我必须提示用户输入三角形的底边和高度作为浮点数,传递给它一个函数,该函数将获取三角形的面积,将其返回给 main。问题是该区域的输出为 0.000000。

它也给了我一个警告

Severity    Code    Description Project File    Line    Suppression State
Warning C4477   'printf' : format string '%f' requires an argument of type 'double', but variadic argument 1 has type 'float (__cdecl *)(float,float)'  line 38. 

我做错了什么?

    #include <stdio.h>
    #include <stdlib.h>


float area(float base,float height);


int main()
{   

float height;
printf("Enter an height: ");
scanf_s("%f", &height);
printf("Number = %f", height);

float base;
printf("Enter an base: ");
scanf_s("%f", &base);
printf("Number = %f", base);

area(height, base);

printf("area of triangle : %f\n", area);

return 0;

}

float area(float base, float height)
{

float half = .5;
float area = half * base * height;

return area;

}

【问题讨论】:

  • area(height, base); printf("area of triangle : %f\n", area); --> printf("area of triangle : %f\n", area(base, height));
  • area 是一个函数。您使用area(height, base) 调用它一次,但您从未将结果分配给变量。然后,当您尝试将area 传递给printf 时,您传递的是一个函数,而不是该函数的结果。
  • 另外:绝对没有理由拥有 halfarea 临时变量。是的,.5 之类的幻数不好,但将其提取到名为 half 的变量中不会添加任何额外信息。
  • 我只需要在记录中说:“在知道如何调用函数之前可以both printf()scanf() 的任何人都可能隐藏了一些某处的辉煌......

标签: c function


【解决方案1】:

您的主要问题是您传递的是一个函数 (area),而不是函数调用的结果 (area(height, base))。您需要将结果存储到一个变量中,然后打印该变量。

float computedArea = area(height, base);
printf("area of triangle : %f\n", computedArea);

或者您可以就地调用函数,这在这种情况下有效,因为它不会使行太长:

printf("area of triangle : %f\n", area(height, base));

以下是我将如何编写此代码:

#include <stdio.h>
#include <stdlib.h>

double area(double base,double height);

int main() {  
    printf("Enter the height: ");
    double height;
    scanf("%lf", &height);
    printf("Height: %f\n", height);

    printf("Enter the base: ");
    double base;
    scanf("%lf", &base);
    printf("Base: %f\n", base);

    double computedArea = area(height, base);

    printf("Triangle Area: %f\n", computedArea);
    return 0;
}

double area(double base, double height) {
    return (base * height) / 2.0;
}

【讨论】:

  • @ebyrob printfscanf 具有不同的格式字符串规范。 %fdouble 代表 printf,但 float 代表 scanf。要让scanf 读入double,您需要“长浮点”%lf 令牌
  • 我的错。我知道我不在生产代码中使用scanf() 是有原因的。我认为建议更改已经正确的代码可能有点过分,但绝对值得指出这里的困境。
  • "您需要将结果存储到一个变量中,然后打印该变量。"误导了它的必要性,因为printing directly the function result 是可以的。首先保存它确实可以提高清晰度。
  • 只是为了记录,%lf 从 C99 开始在 printf 中被允许,但 l 被忽略,除了与 scanf 的一点一致性之外,什么也没有添加。
【解决方案2】:

改变

area(height, base); // invoking a function without capturing its output

printf("area of triangle : %f\n", area); // area refers to the memory location where the function resides.

printf("area of triangle : %.2f\n", area(height, base));
 // Directly passing the area output to printf. The '.2' specifies the precision you want

【讨论】:

  • 我不确定这如何增加新内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2014-01-21
  • 2014-11-10
  • 2014-10-13
相关资源
最近更新 更多