gpt4 book ai didi

c - 函数原型(prototype)问题

转载 作者:行者123 更新时间:2023-11-30 21:24:51 26 4
gpt4 key购买 nike

谢谢大家的建议!我不再在 switch 语句中使用该函数,但现在它给了我 fatal error 。有谁知道为什么现在无法编译?我是否还在原型(prototype)设计或定义错误?再次提前致谢!

uhunix:/home04/y/yingkit/ee150% gcc functions2.c 
Undefined first referenced
symbol in file
largest_of_three /var/tmp//ccEk23i2.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

这是完整的代码:

#include <stdio.h>
#include <math.h>
//include macro functions if any


int main(void)
{
int repeat = 1;
int option = 0;

while(repeat == 1)
{
printf("\nOPTIONS:\n\n");
printf("1. Find the largest of three numbers.\n");
printf("2. Calculate the factorial of a number.\n");
printf("3. Truncate a number.\n");
printf("4. Round a number.\n");
printf("5. Find the inverse of a number.\n");
printf("0. Exit the program.\n\n");
printf("What do you want to do\?\n");

scanf("%i", &option);

float largest_of_three(float, float, float); //prototype

if(option == 0)
{
break;
}

if(option == 1)
{

float x = 0;
float y = 0;
float z = 0;
float result = 0;

printf("First number: ");
scanf("%f", &x);
printf("Second number: ");
scanf("%f", &y);
printf("Third number: ");
scanf("%f", &z);

result = largest_of_three(x, y, z); //calling

float largest_of_three(float x, float y, float z)
{
float w = 0;
if(x > y && x > z)
{
w = x;
}
else
{
if(y > x && y > z)
{
w = y;
}
else //middle
{
if(z > x && z > y)
{
w = z;
}
else
{
printf("There is no single greatest number.\n");
}
} //end middle else
} //end outer else
printf("The greatest number is %f.", w);
return w;
} //end largest_of_three function
} //end option 1
getchar( ); //to prevent buffer issues
printf("Would you like to do another operation\? Type y for yes, n for no.\n");

char yesno = 'y';
scanf("%c", &yesno);
if(yesno == 'y' || yesno == 'Y')
{
repeat = 1;
}
else
{
repeat = 0;
}


} //end while loop

return 0;
}

最佳答案

第 57 行的代码非法。函数不能在 ISO C 中的其他函数内部定义。

该错误表明您的编译器具有允许嵌套函数的扩展,从而为它们提供了内部链接。但你给出了一个带有外部链接的原型(prototype),不匹配。

要解决此问题,请停止使用嵌套函数。 (您可能可以通过在原型(prototype)的开头添加 static 来修复它,但这是不好的编码风格)。

关于c - 函数原型(prototype)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215696/

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