从C

时间:2016-03-20 07:09:44

标签: c

所以我正在为学校做作业,但我一直在收到错误信息,而且我已阅读多篇帖子,但我还没有找到灵魂,或者我只是不明白什么&# 39;据说。

这是我的代码:

#include <stdio.h>

int main() {
//declare the variables
float radius, circumference;

radius = 0;

printf ("This program will calculate the circumference of a circle given the radius\n");

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

circumference = calculate_circumference(radius);

printf ("The circumference of the circle is:\n", circumference);
return 0;
}

float calculate_circumferene (float circle_radius)
{

    float circle_circumference, PI;

    PI = 3.14159;

    circle_circumference = 2 * PI * circle_radius;

    return (circle_circumference);
}

以下是我收到的错误消息:

prog.c: In function 'main':
prog.c:21:18: warning: implicit declaration of function  'calculate_circumference' [-Wimplicit-function-declaration]
  circumference = calculate_circumference(radius);
              ^
prog.c:24:10: warning: too many arguments for format [-Wformat-extra-args]
  printf ("The circumference of the circle is:\n", circumference);
      ^
/home/j1N8a0/ccUei3id.o: In function `main':
prog.c:(.text.startup+0x45): undefined reference to `calculate_circumference'
collect2: error: ld returned 1 exit status

任何类型的帮助或指向我正确的方向将不胜感激!

1 个答案:

答案 0 :(得分:2)

你必须在使用之前声明函数,并注意拼写错误。

虽然为printf()提供额外参数无害,但您必须使用以%开头的格式说明符,才能通过printf()在变量中打印数据。

试试这个:

#include <stdio.h>

/* declare the function to use */
float calculate_circumference (float circle_radius);

int main() {
    //declare the variables
    float radius, circumference;

    radius = 0;

    printf ("This program will calculate the circumference of a circle given the radius\n");

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

    circumference = calculate_circumference(radius);

    /* add format specifier to print the value */
    printf ("The circumference of the circle is: %f\n", circumference);
    return 0;
}

/* add "c" before last "e" of the function name */
float calculate_circumference (float circle_radius)
{

    float circle_circumference, PI;

    PI = 3.14159;

    circle_circumference = 2 * PI * circle_radius;

    return (circle_circumference);
}