我将如何设置函数来计算圆的面积 - C

How would I set up function to calculate the area of a circle - C

我有这个 C 程序需要计算用户输入的圆的面积。 我必须在主要功能之前使用它:

void area_circum(double radius, double *area, double *circum);

我无法让程序运行。我正在使用主要功能和另一个名为 area_circum();

这是我现在拥有的:

#include <stdio.h>


void area_circum(double radius, double *area, double *circum);

int main(void) {

    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%f", &radius);

    area_circum(radius, area, circum);

    printf("Area of circle : %0.4f\n", area);

    return 0;
}


void area_circum(double radius, double *area, double *circum) {
    double PIE = 3.141;

    double areaC = 0;

    areaC = PIE * radius * radius;
}

当我构建它并 运行 它时,它起作用了,我输入了一个数字,但随后它 returns 0.00

#include <stdio.h>

void area_circum(double radius);

int main() {

   double radius;

   printf("Please enter the radius of the circle: ");

   scanf("%lf", &radius);

   area_circum(radius);

   return 0;

}


void area_circum(double radius) {

   double PIE = 3.141;

   double areaC = 0;

   areaC = PIE * radius * radius;

   printf("Area of circle : %0.4f\n", areaC);

}

你快到了。您需要做的就是 return 计算出的值:

void area_circum(double radius, double *area) {
    double PIE = 3.141;
    *area = PIE * radius * radius;
}

并像这样打电话:

    area_circum(radius, &area);

从函数的名称和签名可以看出,它应该同时计算面积和周长。同样很明显,您应该将两个 int 变量(或两个指向 int 的指针)的地址传递给它,然后将给出计算结果:

#include <stdio.h>

void area_circum(double radius, double *area, double *circum);

int main(void)
{
    double radius, area, circum;

    printf("Please enter the radius of the circle: ");
    scanf("%lf", &radius); // Note: You should add a check to see if
                           //       scanf() failed.
    area_circum(radius, &area, &circum);  // &area is the address of area
                                          // &circum is the address of circum
    printf("Area of circle : %0.4f\n", area);
    printf("Circumference of circle : %0.4f\n", circum);
    return 0;
}

void area_circum(double radius, double *area, double *circum)
{
    double PIE = 3.1416;
    *area = PIE * radius * radius;
    *circum = PIE * radius * 2;
}