Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i fix this runtime error for a simple code written in c?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Enter the circle radius : 20
you entered 20.000000
area is 1256.000000
area > 1000

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading