我的gcc编译器给了我一个错误,显示为(函数)的冲突类型

时间:2016-04-16 10:40:53

标签: c compilation compiler-errors

我是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;
}

4 个答案:

答案 0 :(得分:2)

你宣布了你的功能&#34; ar&#34;返回类型为float但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

答案 1 :(得分:1)

问题是您在原型中指定了返回类型,但在实际的函数定义中却没有。当您使用特定定义编写原型时,在编写实际函数时必须遵循相同的定义。

我还将整数加法转换为浮点数,以便您可以正确计算面积。当你取一个整数并用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;
}

这将干净利落地编译。

答案 2 :(得分:0)

我试着在这里加入一些解释:

#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;
    }

答案 3 :(得分:0)

您应该在函数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;
}