I'm getting a "conflicting types" error in C

12,867

Have you declared the function rectangle() before it is used? If not, it will be assumed to return an int.

You need a line like:

double rectangle(void);

somewhere before you call it, or to define the function in the same module from which it is called, before it is called.

Share:
12,867
aakbari1024
Author by

aakbari1024

Updated on November 20, 2022

Comments

  • aakbari1024
    aakbari1024 over 1 year

    I'm currently working on a program for a C course in which I have to output the area of a shape.

    Here is a function for a rectangle's area that I have in my program:

    double rectangle() // calculate area of rectangle
    {
        double length, width;
    
        printf("\nEnter length and width of rectangle: ");
        scanf("%g %g\n", &length, &width);
    
        return (length*width);
    }
    

    here is where I call the function rectangle()

    if(strncmp(shape, "rectangle", 15) == 0)
        area = rectangle();
    

    I'm using Geany in Linux Mint with the GCC compiler.

    The error I'm recieving is

    "geometryv2.c:78: error: conflicting types for ‘rectangle’"

    I don't see what's conflicting here. The function with return-type double is returning a double. Any help here would be greatly appreciated. I am still pretty new to C and this is actually my first C program.

    Thanks!

    • zneak
      zneak over 13 years
      You probably have declared the function elsewhere with a different return type.
    • aakbari1024
      aakbari1024 over 13 years
      No, that's the only place where the function is declared, and this is happening with all my other functions. I have "triangle" "ellipse" "circle" and "polygon" functions that are all giving the same error.
    • William Pursell
      William Pursell over 13 years
      In the mid 1980's, it was considered okay to use an empty argument list in the declaration (and definition) of the function. Since 1989, it is best to explicitly indicate that no arguments are accepted by using the keyword 'void'. In other words, 'double rectangle()' is accepted by compilers only for backwards compatibility, but all new code should be written 'double rectangle( void )'.
  • aakbari1024
    aakbari1024 over 13 years
    well all my functions are after main, is that the problem?
  • James McLeod
    James McLeod over 13 years
    Defining the functions after is fine, but you need to declare them (provide their prototypes - return type, name, and arguments) before they are called.
  • zneak
    zneak over 13 years
    aakbari1024 Chances are it is. Unless you explicitly declare them ([return-type] [function-name]([arguments]);), you should define them before your main.
  • Bart van Ingen Schenau
    Bart van Ingen Schenau over 13 years
    @aakbari1024: Yes, that is the problem. Add a declaration of your functions before main, like James showed, and the problem is fixed.
  • aakbari1024
    aakbari1024 over 13 years
    Alright, I declared the functions before main. But now I'm getting this error: geometryv2.c:(.text+0x2c5): undefined reference to `sqrt' That's in my "triangle()" function. I included the "math.h" library. Any reason why this is happening?
  • James McLeod
    James McLeod over 13 years
    That sounds like it might be a linker error. Are you linking in the standard C library when you build the executable?
  • Georg Fritzsche
    Georg Fritzsche over 13 years
    @aak: You need to link to it too using -lm. See e.g. here.
  • James McLeod
    James McLeod over 13 years
    Trust me - it gets easier! Although there are a lot of these apparently silly details you need to deal with when you are starting to learn a language as rich as C, there a also a lot of helpful people trapped in the Internet waiting to help you navigate your way to experthood.
  • William Pursell
    William Pursell over 13 years
    @aakbari1024: 'math.h' is not a library. It is a header file that declares the functions available in the math library. It only provides declarations, but does not provide definitions.
  • Casual Coder
    Casual Coder over 13 years
    obviously, only relevant part, that is formatting, other argument are also missing if you failed to notice. %g is for float data type, %lg is for double data type as in question.