[英]My gcc compiler gives me an, error that shows as Conflicting types for (function)
我是C语言的新手,我制作了此程序,但出现错误“(函数)的类型冲突”“(函数)的先前声明在这里”。
我在系统上使用命令提示符使用Dev c ++的gcc编译器对此进行了编译。 有人可以帮我了解我的错吗?
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
ar(a,b,c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
您使用返回类型float声明了函数“ ar”,但返回了float ar(int a,int b,int c); 但您没有定义返回值。这在这里引起了问题。 尝试这个:
#include<stdio.h>
#include<math.h>
float ar(int a,int b,int c);
void main()
{
int a,b,c;
float area;
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(int a,int b,int c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
使用命令:gcc -std = c99 -o stack_16_4_16 stack_16_4_16.c -lm编译
问题在于您在原型中指定了返回类型,但未在实际函数定义中指定返回类型。 当使用特定定义编写原型时,编写实际函数时必须遵循相同的定义。
我还将整数加法转换为浮点数,以便可以正确计算面积。 当您使用整数并将其除以C中的浮点数时,无论您将其分配给什么,都将得到一个整数。 演员表将改变这种行为。
考虑一下:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(int a,int b,int c)
{
float area,s;
s=(float)(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
这将干净地编译。
我试图在这里包括一些解释:
#include<stdio.h>
#include<math.h>
float ar(int a,int b,int c);
/* This is a function declaration and just like variables, functions are
* also limited by scope. If you declare the function inside the main,
* then the function cannot be called outside the main. That is the purpose
* it is declared here.
*/
int main()
{
int a,b,c;
float area;
printf("Enter the lengths of the three sides of a triangle : ");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is = %.2f",area);
return 0;
}
float ar(int a,int b,int c)
/* Note that I have added the types of arguments
* In older versions of C it is not uncommon to see functions like
* float ar(a,b)
* int a,b;
* {Some Stuff Here}
* The above function style was problematic - it couldn't deal with mismatched arguments.
* So it is a good practice to specify the type of the parameters.
* In fact this was ANSI C standard's solution to the problems of mismatched arguments.
*/
{
float area,s;
s=(a+b+c)/(float)2;
/* Either the numerator or the denominator should be float for the output to be float
* So I casted the denominator to a float value. Also, if you're using Heron's formula
* the denominator should be 2 , not 3.
*/
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
您应该在函数ar中添加返回类型
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(float a, float b, float c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.