Please help with newbie error

S

somebody

Okay, I bought "C in a Nutshell", and attempted to compile
the first example. When I attempt to compile this simple
program, I get these two errors:


gcc circle.c -o circle
circle.c:29: error: conflicting types for 'circularArea'
circle.c:13: error: previous implicit declaration of 'circularArea' was here


I've included the actual program below. They types are all doubles,
so I don't see how there are any conflicting types.



#include <stdio.h>


int main()
{

double radius = 1.0, area = 0.0;

printf( " Areas of Circles\n\n" );
printf( " Radius Area\n"
"-------------------------\n" );

area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );

radius = 5.0;
area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );

return 0;
}


/*The function circularArea( ) calculates the area of a circle*/
/*Parameter: The radius of the circle*/
/*Return value: The area of the circle*/

double circularArea( double r ) // Definition of circularArea( ) begins
{
const double pi = 3.1415926536; // Pi is a constant
return pi * r * r;
}
 
A

Anthony Irwin

somebody said:
Okay, I bought "C in a Nutshell", and attempted to compile
the first example. When I attempt to compile this simple
program, I get these two errors:

Not sure what others will think about this book. There are a lot of
bad books out there and unfortunately I have bought many of them.

gcc circle.c -o circle

gcc -std=c89 -Wall circle.c -o circle

Will give you the warnings also and compile with c89 standard for c99
use c99. -pedantic will not use gcc compiler extensions if you want to
ensure that you are using standards compliant code.

circle.c:29: error: conflicting types for 'circularArea'
circle.c:13: error: previous implicit declaration of 'circularArea' was here
#include <stdio.h>


int main()
{

double radius = 1.0, area = 0.0;
double circularArea( double r );

/* The above code is the function prototype that tells c
about the function that you have declared at the end of this

code */

kind regards,
Anthony Irwin
 
U

user923005

/*
The problem was that circularArea() was defined after main().
Since you failed to provide a prototype for circularArea(), C assumed
int.
Since it turned out to be a double, C became upset with you and let
you
know "in NO UNCERTAIN WORDS" that you had pulled a bozo-nono.

If you typed in the program exactly as found in the book, then the
book is broken.
If you typed the functions in opposite order, then it's your mistake.
You could also have fixed it by providing a prototype for
circularArea().
*/
#include <stdio.h>

/*The function circularArea( ) calculates the area of a circle*/
/*Parameter: The radius of the circle*/
/*Return value: The area of the circle*/
double circularArea( double r ) // Definition of circularArea( )
begins
{
const double pi = 3.1415926536; // Pi is a constant

return pi * r * r;
}

int main(void)
{
double radius = 1.0, area = 0.0;

printf( " Areas of Circles\n\n" );
printf( " Radius Area\n"
"-------------------------\n" );
area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );
radius = 5.0;
area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );
return 0;
}
 
O

osmium

somebody said:
Okay, I bought "C in a Nutshell", and attempted to compile
the first example. When I attempt to compile this simple
program, I get these two errors:


gcc circle.c -o circle
circle.c:29: error: conflicting types for 'circularArea'
circle.c:13: error: previous implicit declaration of 'circularArea' was
here

Functions without prototypes implicitly return an int. That's what you have
here.
I've included the actual program below. They types are all doubles,
so I don't see how there are any conflicting types.



#include <stdio.h>

One cure is to put your code for the circular function here - that's what I
normally do. . The other is to put a function protype here. Like this:

int circular Area(double);

The semicolon makes it a prototype. It is a promise of things to come.
 
C

CBFalconer

somebody said:
Okay, I bought "C in a Nutshell", and attempted to compile
the first example. When I attempt to compile this simple
program, I get these two errors:

gcc circle.c -o circle
circle.c:29: error: conflicting types for 'circularArea'
circle.c:13: error: previous implicit declaration of 'circularArea' was here

I've included the actual program below. They types are all doubles,
so I don't see how there are any conflicting types.

#include <stdio.h>

int main()
{
double radius = 1.0, area = 0.0;

printf( " Areas of Circles\n\n" );
printf( " Radius Area\n"
"-------------------------\n" );

area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );

radius = 5.0;
area = circularArea( radius );
printf( "%10.1f %10.2f\n", radius, area );

return 0;
}

/*The function circularArea( ) calculates the area of a circle*/
/*Parameter: The radius of the circle*/
/*Return value: The area of the circle*/

double circularArea( double r ) // Definition of circularArea( ) begins
{
const double pi = 3.1415926536; // Pi is a constant
return pi * r * r;
}

Move the declaration of circularArea before the declaration of
main. C requires declaration before use. By using it undeclared
(you have a C90 compiler, C99 would have complained) you have
implicitly declared it as returning double, which it doesn't.
 
A

Anthony Irwin

osmium said:
One cure is to put your code for the circular function here - that's what I
normally do. . The other is to put a function protype here. Like this:

int circular Area(double);

You have a typo should be double circularArea(double);


Kind Regards,
Anthony Irwin
 
S

somebody

Oops! It was my fault -- I missed the function prototype:

double circularArea( double r ); // Function declaration (prototype form)
 
S

somebody

Functions without prototypes implicitly return an int. That's what you have
here.

There WAS a function prototype that I missed in the book:

double circularArea( double r ); // Function declaration (prototype form)


-Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top