gpt4 book ai didi

c - 从 C 中的类型分配给类型时出错,类型不兼容

转载 作者:行者123 更新时间:2023-11-30 18:51:45 27 4
gpt4 key购买 nike

所以我正在为学校做作业,但我不断收到错误消息,并且我已经阅读了多个线程,但我还没有找到灵魂,或者也许我只是不明白所说的内容。

这是我的代码:

#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

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

最佳答案

在使用函数之前必须先声明函数,并小心拼写错误。

尽管为 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);
}

关于c - 从 C 中的类型分配给类型时出错,类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36111118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com