gcc编译器给我一个错误,显示为(函数)的冲突类型
我是C语言的新手,我做了这个程序,我得到了一个错误 “针对(函数)的冲突类型” “以前的(功能)声明在此”gcc编译器给我一个错误,显示为(函数)的冲突类型,c,compilation,compiler-errors,C,Compilation,Compiler Errors,我是C语言的新手,我做了这个程序,我得到了一个错误 “针对(函数)的冲突类型” “以前的(功能)声明在此” 我使用GDCC编译器编译DEVC++,在我的系统上使用命令提示符。 有人能帮我理解我的错误吗 #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
我使用GDCC编译器编译DEVC++,在我的系统上使用命令提示符。 有人能帮我理解我的错误吗
#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;
}
#包括
#包括
main()
{
INTA、b、c;
漂浮区;
浮动ar(整数a、整数b、整数c);
printf(“输入三角形三条边的长度”);
scanf(“%d%d%d”、&a、&b和&c);
面积=ar(a、b、c);
printf(“三角形的a为%.2f”,面积);
}
应收账款(a、b、c)
{
漂浮区,s;
s=(a+b+c)/3;
面积=平方米(s*(s-a)*(s-b)*(s-c));
返回区;
}
问题在于您在原型中指定了返回类型,但在实际函数定义中没有指定。当您使用特定定义编写原型时,在编写实际函数时必须遵循相同的定义
我还将整数加法转换为浮点数,以便正确计算面积。在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;
}
#包括
#包括
int main()
{
INTA、b、c;
漂浮区;
浮动ar(整数a、整数b、整数c);
printf(“输入三角形三条边的长度”);
scanf(“%d%d%d”、&a、&b和&c);
面积=ar(a、b、c);
printf(“三角形的a为%.2f”,面积);
}
浮动ar(整数a、整数b、整数c)
{
漂浮区,s;
s=(浮动)(a+b+c)/3;
面积=平方米(s*(s-a)*(s-b)*(s-c));
返回区;
}
这将干净地编译 您使用返回类型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;
}
#包括
#包括
浮动ar(整数a、整数b、整数c);
void main()
{
INTA、b、c;
漂浮区;
printf(“输入三角形三条边的长度”);
scanf(“%d%d%d”、&a、&b和&c);
面积=ar(a、b、c);
printf(“三角形的a为%.2f”,面积);
}
浮动ar(整数a、整数b、整数c)
{
漂浮区,s;
s=(a+b+c)/3;
面积=平方米(s*(s-a)*(s-b)*(s-c));
返回区;
}
使用以下命令编译:gcc-std=c99-o stack_16_4_16 stack_16_4_16.c-lm我试图在这里包括一些解释:
#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(整数a、整数b、整数c);
/*这是一个函数声明,就像变量一样,函数也是
*也受范围限制。如果在main中声明函数,
*那么就不能在main之外调用该函数。这就是目的
*这是在这里宣布的。
*/
int main()
{
INTA、b、c;
漂浮区;
printf(“输入三角形三条边的长度:”);
scanf(“%d%d%d”、&a、&b和&c);
面积=ar(a、b、c);
printf(“三角形的a为%.2f”,面积);
返回0;
}
浮动ar(整数a、整数b、整数c)
/*注意,我已经添加了参数的类型
*在旧版本的C中,经常会看到这样的函数
*浮动ar(a、b)
*INTA,b;
*{这里有些东西}
*上面的函数样式有问题-它无法处理不匹配的参数。
*因此,指定参数的类型是一种很好的做法。
*事实上,这是ANSI C标准对不匹配参数问题的解决方案。
*/
{
漂浮区,s;
s=(a+b+c)/(浮动)2;
/*分子或分母都应该是float,输出应该是float
*所以我把分母转换成了一个浮点值。同样,如果你用的是Heron公式
*分母应该是2,而不是3。
*/
面积=平方米(s*(s-a)*(s-b)*(s-c));
返回区;
}
您应该在函数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;
}
#包括
#包括
main()
{
INTA、b、c;
漂浮区;
浮动ar(整数a、整数b、整数c);
printf(“输入三角形三条边的长度”);
scanf(“%d%d%d”、&a、&b和&c);
面积=ar(a、b、c);
printf(“三角形的a为%.2f”,面积);
}
浮子ar(浮子a、浮子b、浮子c)
{
漂浮区,s;
s=(a+b+c)/3;
面积=平方米(s*(s-a)*(s-b)*(s-c));
返回区;
}
你读过基本的C语言书或教程吗?它字面上说的是“函数的冲突类型”吗????或者你出于某种原因对我们隐瞒了函数名。它说'ar'的类型冲突以前的'ar'声明就在这里。我怀疑它是在抱怨ar(a,b,c)
缺少返回规范。你最好寻找更好的学习材料ar(a,b,c)
这是一种非常糟糕的函数声明方法。相反,用完整的类型声明它:float ar(inta,intb,intc){…}
。通过不声明返回类型,编译器假定它是int
。这与main
中函数的声明相冲突(这也是不好的做法)。很好,现在得到了,将永远记住,感谢不够,我想这可能是我以前的程序中的一个错误。谢谢。我需要的就是投票和接受。)虽然这是汇编,但它的风格很差。参数应声明为inta、intb、intc
。此外,由于原型位于main
内部,如果定义不匹配(例如,原始代码中的情况),编译器实际上不需要诊断错误。最好在main
之前有原型。