C Student Assignment, ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’

55,017

Solution 1

Change

scanf("%f", carpetCost);

with

scanf("%lf", carpetCost);

%f conversion specification is used for a float * argument, you need %lf for double * argument.

Solution 2

Use %lf specification instead for double * argument.

scanf("%lf", carpetCost); 

Solution 3

Use %lf instead of %f if you are scanning a double type of variable. You can check this in detail also about %lf & %f from the discussed thread's link.

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Share:
55,017
David Peterson Harvey
Author by

David Peterson Harvey

I own a small media production company where I manage a recording studio and a several business web pages.

Updated on July 09, 2022

Comments

  • David Peterson Harvey
    David Peterson Harvey almost 2 years

    I'm working on an assignment and I'm getting this warning:

    C4_4_44.c:173:2: warning: format ‘%f’ expects argument of type ‘float *’, 
    but argument 2 has type ‘double *’ [-Wformat]
    

    The variabled is declared in main as:

    double carpetCost;
    

    I'm calling the function as:

    getData(&length, &width, &discount, &carpetCost);
    

    And here's the function:

    void getData(int *length, int *width, int *discount, double *carpetCost)
    
    {
    
        // get length and width of room, discount % and carpetCost as input
    
        printf("Length of room (feet)? ");
    
        scanf("%d", length);
    
        printf("Width of room (feet)? ");
    
        scanf("%d", width);
    
        printf("Customer discount (percent)? ");
    
        scanf("%d", discount);
    
        printf("Cost per square foot (xxx.xx)? ");
    
        scanf("%f", carpetCost);
    
        return;
    
    } // end getData
    

    This is driving me crazy because the book says that you don't use the & in

    scanf("%f", carpetCost); 
    

    when accessing it from a function where you passed it be reference.

    Any ideas what I'm doing wrong here?