Search code examples
c

c programming: answer always equates to 0


#include <stdio.h>

int main(){

    double Radius, Circumference, Area, Pi = 3.1416;
    
    printf("Value of Radius:");
    scanf("%lf",Radius);
    
    Area = Pi * Radius * Radius;
    Circumference = 2 * Pi * Radius;
    
    printf("The area of the circle is %lf", Area);
    printf("\nThe circumference of the circle is %lf", Circumference);
    
    return 0; 
}

I want to be able to get both circumference and area by just plugging in the radius.


Solution

  • The scanf function needs a pointer.

    scanf("%lf", &Radius);
    

    You should have received a warning (enable warnings!) on compiling this:

    test.c: In function ‘main’:
    test.c:8:14: warning: format ‘%lf’ expects argument of type ‘double *’, but argument 2 has type ‘double’ [-Wformat=]
         scanf("%lf",Radius);
                ~~^
    

    You should also check the return value of scanf to ensure it actually read the correct number of values.