简体   繁体   English

C 程序总是给我同样的 output 0.00?

[英]C program keeps giving me the same output of 0.00?

I'm new to c, and my input is 16.7 and 28.6, this is my code, I need to know why I'm only getting 0.00 as an answer, has to save height radius and pi as double's any help is appreciated.我是 c 的新手,我的输入是 16.7 和 28.6,这是我的代码,我需要知道为什么我只得到 0.00 作为答案,必须保存高度半径和 pi 作为 double 的任何帮助表示赞赏。

#include <stdio.h>
#include <math.h>


float coneSurfaceArea(double radius, double height, const double pi);


int main()
{
    double radius;
    double height;
    const double pi = 3.14;
    
    printf("Enter the radius: ");
    scanf("%lf", &radius);
    printf("Enter the height: ");
    scanf("%lf", &height);
    printf("The total surface area of the cone is %.2f", coneSurfaceArea);
    return 0;
}

float coneSurfaceArea(double radius, double height, const double pi)
{
    float SA = pi * radius * (radius + sqrt(radius * radius + height * height));
    return SA;
}

Whatever compiler you're using, turn on its optional warnings (diagnostics, whatever your compiler calls them), No halfway decent compiler would accept this code without complaining.无论您使用什么编译器,打开它的可选警告(诊断,无论您的编译器如何调用它们),没有半途而废的编译器会毫无怨言地接受此代码。 but unfortunately many compilers default to emitting very few warnings.但不幸的是,许多编译器默认发出很少的警告。

GCC does point out the problem, even by default. GCC 确实指出了问题,即使默认情况下也是如此。

a.c:18:54: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float (*)(double,  double,  const double)’ [-Wformat=]
   18 |     printf("The total surface area of the cone is %.2f", coneSurfaceAre);
      |                                                   ~~~^   ~~~~~~~~~~~~~~
      |                                                      |   |
      |                                                      |   float (*)(double,  double,  const double)
      |                                                      double

In case you're not familiar with the syntax: float (*)(…) is the type of a pointer to a function taking arguments as described inside the second pair of parentheses, and returning a float value.如果您不熟悉语法: float (*)(…)是指向 function 的指针的类型,采用第二对括号中描述的 arguments 并返回一个float值。 coneSurfaceAre is a function, and since C mostly doesn't manipulate functions as such, a function is treated as a pointer to the function in most contexts. coneSurfaceAre is a function, and since C mostly doesn't manipulate functions as such, a function is treated as a pointer to the function in most contexts.

The printf function expects a value of type double , but you're passing a pointer to a function. printf function 需要一个double类型的值,但您传递的是指向 function 的指针。 Depending on the system, this may result in printing a meaningless value, a crash, the program continuing but with corrupted memory, etc.根据系统的不同,这可能会导致打印无意义的值、崩溃、程序继续但 memory 损坏等。

When you want the value returned by a function, you need to call this function, passing it arguments.当您想要 function 返回的值时,您需要调用此 function,并将其传递给 arguments。 Using the same names for variables in the main function and in the coneSurfaceArea function may help humans, but as far as the computer is concerned, these are completely different variables.main function 和coneSurfaceArea区域 function 中的变量使用相同的名称可能对人类有帮助,但就计算机而言,这些是完全不同的变量。

    printf("The total surface area of the cone is %.2f",
           coneSurfaceArea(radius, height, pi));

You could pass other values if you wanted.如果需要,您可以传递其他值。

    printf("The total surface area of a cone with twice the height is %.2f",
           coneSurfaceArea(radius, 2 * height, pi));

By the way, printf with the %f format expects a double argument¹.顺便说一句, %f格式的 printf 需要一个double参数¹。 Here, you're passing a float .在这里,您传递了一个float This is actually correct because of promotions : when you pass certain “small” types to a function like printf that takes a variable number of arguments, they are promoted to a larger type, eg short to int and float to double .由于促销,这实际上是正确的:当您将某些“小”类型传递给int (如printf ,它采用可变数量的short ,它们被提升为更大的类型,例如,浮点型和float double But in more complex cases you could run into problems if you aren't careful.但是在更复杂的情况下,如果您不小心,您可能会遇到问题。 It's generally simpler to stick to double .坚持使用通常更简单double And using double all the time is often slightly faster than using float , because using float just results in the same calculations followed by a conversion.并且一直使用double通常比使用float稍微一点,因为使用float只会导致相同的计算,然后进行转换。 There is an advantage to float when the program manipulates a lot of numbers, because then you can save memory and, in some cases, take advantage of vector instructions that can perform two independent float operations just as fast as one double operation.当程序处理大量数字时, float有一个优势,因为这样您可以节省 memory,并且在某些情况下,可以利用向量指令来执行两个独立的float运算,就像一个double精度运算一样快。

¹ Unlike scanf , which does need a pointer to float for %f , and a pointer to double for %lf . ¹scanf不同,它确实需要一个指向float%f指针和一个指向double%lf的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM