Reputation: 37
In my code, squaring the variable R
always gives zero.
It does not matter if it's pow(R, 2)
or R*R
.
Why does that happen?
(If I change it to float, it works, but I need it to be a double for the precision.)
This is the code:
int main () {
double R,PI,a;
PI = 3.14159;
scanf(" %d", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4d \n", a );
return 0;
}
Upvotes: 1
Views: 122
Reputation: 224917
You're using the wrong format specifier in scanf
and printf
.
The %d
format specifier expects the address of an int
for scanf
, and an int
for printf
. You're instead passing in the address of a double
and a double
respectively. Using the wrong format specifiers invokes undefined behavior.
You need to use %lf
in scanf
and %f
in printf
for a double
:
scanf("%lf", &R);
a = PI * (pow(R,2));
printf ( "A= %0.4f \n", a );
Upvotes: 2