My code shows me a wrong output by showing the area to be zero:
#include <stdio.h>
double calc(float rad);
void main(void) {
float rad;
printf("Enter the circle radius: \n");
scanf("%f",&rad);
printf("You entered: %f\n",rad);
printf("Area is %d\n",calc(rad));
if(calc(rad)>1000){
printf("Area is > 1000");
}
else{
printf("Area is < 1000");
}
}
double calc(float rad){
double area=3.14*rad*rad;
return area;
}
Output:
Enter the circle radius : 20
you entered 20.000000
area is 0
area > 1000
Desired output:
Enter the circle radius : 20
you entered 20.000000
area is 1256.000000
area > 1000
CodePudding user response:
The erroneous output is as a result of using %d
instead of %lf
for double or %f
for float in your printf
statement. Turning on compiler warning -Wformat
will highlight this during compilation
CodePudding user response:
#include <stdio.h>
double calc(float rad);
void main(void) {
float rad;
printf("Enter the circle radius: \n");
scanf("%f",&rad);
printf("You entered: %d\n",rad);
printf("Area is %f\n",calc(rad));
if(calc(rad)>1000){
printf("Area is > 1000");
}
else{
printf("Area is < 1000");
}
}
double calc(float rad){
double area=3.14*rad*rad;
return area;
}
This is the correct code. if you want more information :
https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html
https://www.cplusplus.com/reference/cstdio/printf/
https://aticleworld.com/format-specifiers-in-c/ Please go to the last link first.I am sorry for any bad english.