Search code examples
cdoublescanfconversion-specifier

warning: format ‘%f’ expects argument of type ‘float ’, but argument 2 has type ‘double ’


everyone. I NEED HELP! I was trying to submit this following HackerRank's challenge : Task Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost. Round the result to the nearest integer.

#include <stdio.h>
#include <math.h>
int main()

{
    int tax,tip;
    double mealc;
    
scanf("%f",&mealc);
scanf("d",&tip);
scanf("%d",&tax);
mealc = mealc+(mealc*tip/100))+(mealc*tax/100);
printf ("%d",round(mealc));

    return 0;
}

After compiling the code above. I always get these errors :

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

Hk2.c:37:11: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]

What is the problem ?


Solution

  • As the warning message says the conversion specifier %f is designated to input values for objects of the type float instead of the type double.

    To input a value for an object of the type double you need to use the conversion specifier %lf.

    scanf("%lf",&mealc);
    

    Also you have a typo in this call

    scanf("d",&tip);
    

    you need to write

    scanf("%d",&tip);
    

    And in this statement

    mealc = mealc+(mealc*tip/100))+(mealc*tax/100);
    

    there is a redundant closing parenthesis. You need to write

    mealc = mealc+(mealc*tip/100)+(mealc*tax/100);