Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何输出C中浮点函数返回的值_C_Function - Fatal编程技术网

如何输出C中浮点函数返回的值

如何输出C中浮点函数返回的值,c,function,C,Function,问题是,我必须提示用户以浮点形式输入三角形的底面和高度,并将其传递给函数,函数将获取三角形的面积,然后将其返回到main。问题是该区域的输出为0.000000 这也给了我一个警告 Severity Code Description Project File Line Suppression State Warning C4477 'printf' : format string '%f' requires an argument of type 'double', b

问题是,我必须提示用户以浮点形式输入三角形的底面和高度,并将其传递给函数,函数将获取三角形的面积,然后将其返回到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;

}
#包括
#包括
浮动区域(浮动底座、浮动高度);
int main()
{   
浮动高度;
printf(“输入高度:”);
扫描单位(“%f”和高度);
printf(“数字=%f”,高度);
浮动底座;
printf(“输入基数:”);
scanf_s(“%f”和“基”);
printf(“数字=%f”,基数);
面积(高度、基底);
printf(“三角形面积:%f\n”,面积);
返回0;
}
浮动区域(浮动底座、浮动高度)
{
浮动一半=0.5;
浮动面积=一半*底座*高度;
返回区;
}

您的主要问题是传递的是函数(
区域
),而不是函数调用的结果(
区域(高度、基准)
)。您需要将结果存储到变量中,然后打印该变量

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;
}
#包括
#包括
双面积(双基、双高);
int main(){
printf(“输入高度:”);
双高;
扫描频率(“%lf”和高度);
printf(“高度:%f\n”,高度);
printf(“输入基数:”);
双基;
扫描频率(“%lf”、&base);
printf(“基:%f\n”,基);
双计算面积=面积(高度、基准);
printf(“三角形区域:%f\n”,计算区域);
返回0;
}
双面积(双基、双高){
返回(基准*高度)/2.0;
}
更改

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(“三角形面积:%f\n”,面积)-->
printf(“三角形面积:%f\n”,面积(底部、高度))
区域
是一个函数。使用
区域(高度、基准)
调用一次,但从未将结果指定给变量。然后,当您尝试将
区域
传递给
printf
时,您传递的是一个函数,而不是该函数的结果。此外:绝对没有理由使用
一半
区域
临时变量。是的,像
.5
这样的神奇数字是不好的,但是将它提取到名为
half
的变量中不会添加任何额外的信息。我只需要记录如下:“任何人都可以同时获得
printf()
scanf()
在知道如何调用函数之前,可能在某个地方隐藏了一些光彩…@ebyrob
printf
scanf
有不同的格式字符串规范。
%f
对于
printf
双精度的,但是对于
scanf
则是
float
。要让
scanf
读取
double
,您需要“长浮点”
%lf
标记我的不好。我知道我在生产代码中不使用
scanf()
是有原因的。我认为建议更改已经正确的代码可能会很费力,但这绝对值得指出这里的困难。让我们一起来。“您需要将结果存储到一个变量,然后打印该变量。“误导它的必要性是可以的。不过,先节省它确实可以提高清晰度。只是为了记录,从C99开始,printf中允许使用
%lf
,但是
l
被忽略,除了与scanf的一点一致性之外,没有添加任何内容。我不确定这是如何添加任何新内容的。
printf("area of triangle : %.2f\n", area(height, base));
 // Directly passing the area output to printf. The '.2' specifies the precision you want