Question

I got the following error at compile time.I know it sounds wrong,but what's the exact message compiler is trying to convey: error: conflicting types for 'fun' error: previous decalaration of fun was here:

 int main( )
 {
    extern int fun(float);
    int a;
    a=fun(3.14F);
    printf("%d\n",a);
    return 0;
}

int fun( aa )
float aa;
{
    return( (int) aa);
}
Was it helpful?

Solution

K&R-style function declarations aren't quite the same as modern style ones. In particular, the default argument promotions take place, making your float parameter not quite legal. You have two choices to fix your problem:

  1. Change fun to accept a double parameter instead of a float.

  2. Change the definition of fun to a standard-C-style function definition:

    int fun(float aa)
    {
        return aa;
    }
    

    I also removed the unnecessary cast & parentheses.

As an aside, if you are a beginner, you might find clang helpful - it sometimes provides much better error messages. For your program, for example:

example.c:13:7: warning: promoted type 'double' of K&R function parameter is not
      compatible with the parameter type 'float' declared in a previous
      prototype [-Wknr-promoted-parameter]
float aa;
      ^
example.c:5:25: note: previous declaration is here
    extern int fun(float);
                        ^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top