views:

78

answers:

2

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!

+2  A: 

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.

James McLeod
well all my functions are after main, is that the problem?
aakbari1024
Defining the functions after is fine, but you need to declare them (provide their prototypes - return type, name, and arguments) before they are called.
James McLeod
`aakbari1024` Chances are it is. Unless you explicitly declare them (`[return-type] [function-name]([arguments]);`), you should define them before your `main`.
zneak
@aakbari1024: Yes, that is the problem. Add a declaration of your functions before main, like James showed, and the problem is fixed.
Bart van Ingen Schenau
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?
aakbari1024
That sounds like it might be a linker error. Are you linking in the standard C library when you build the executable?
James McLeod
@aak: You need to link to it too using `-lm`. See e.g. [here](http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html).
Georg Fritzsche
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.
James McLeod
@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.
William Pursell
A: 

What is data type of area variable ?

also fix scanf:

scanf("%lg %lg")
Casual Coder
a semicolon would fix your scanf...
Kedar Soparkar
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.
Casual Coder