Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 1000
David Peterson Harvey Avatar asked Feb 21 '14 20:02

David Peterson Harvey


2 Answers

Change

scanf("%f", carpetCost);

with

scanf("%lf", carpetCost);

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

like image 72
ouah Avatar answered Sep 28 '22 03:09

ouah


Use %lf specification instead for double * argument.

scanf("%lf", carpetCost); 
like image 27
haccks Avatar answered Sep 28 '22 02:09

haccks