Originally Posted by
Kernelpanic
Of course, because your variable declaration doesn't make sense.
circumference = diameter * pie; - is what?
If diameter is 2 times radius, 2 times PI times r is the same as PI times diameter!
And your code uses unecessary headers. Take a look at this simplification:
Code:
// circ.c
//
// Compile with:
// gcc -O2 -o circ circ.c
//
// NOTE: Don't need to link with libm.
//
#include <stdio.h>
#include <stdlib.h> // nedded only for EXIT_FAILURE, EXIT_SUCCESS constants.
// if you use 1 and 0 as return values, stdlib.h isn't necessary.
#include <math.h> // needed only for M_PI constant, otherwise, not needed.
// FIX: Notice using "%lf" conversion is WRONG, except in scanf().
int main( void )
{
double d, p, r, a;
// Since stdout is LINE BUFFERED, you should call fflush().
printf( "Please enter the value of circle radius in cm: " );
fflush( stdout );
// NOTE: You should check if scanf() was abble to do the
// conversion.
if ( scanf( "%lf", &r ) != 1 )
{
error:
fputs( "ERROR: Invalid value.\n", stderr );
return EXIT_FAILURE;
}
if ( r < 0.0 )
goto error;
// NOTE: math.h defines M_PI with double precision.
// Don't need to call pow() from libm!
d = r + r; // 2r = r+r
p = M_PI * d; // 2πr == πd
a = M_PI * r * r; // πr² = πrr
// NOTE: Here %lf is WRONG. To use 'long double' it should be %Lf (uppercase L),
// But, we need only double.
printf( "Diameter : %f cm\n"
"Perimeter: %f cm\n"
"Area : %f cm²\n",
d, p, a );
return EXIT_SUCCESS;
}