Thread: Get rid of compiling warning?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Get rid of compiling warning?

    Code:
    #include <stdio.h>
    double fiveycost(double house, double fuel_cost, double tax_rate);
    int main()
    {
            printf("This program determines the total cost of owning a home for five years.\nThe user will enter initial cost in whole dollars, annual fuel costs in whole dollars, and the annual tax rate as a real number.\n");
    
    
            printf("Please enter the initial cost, fuel cost, and tax rate: ");
            double house;
            double fuel_cost;
            double tax_rate;
            scanf("%lf %lf %lf", &house,&fuel_cost,&tax_rate);
       (Line 13) printf(" The total cost is $%lf.",fiveycost);
            return 0;
    }
    
    
            double fiveycost( double house, double fuel_cost, double tax_rate)
            {
                    double taxes=(house*tax_rate);
                    return (5.0*taxes)+(house)+(5.0*fuel_cost)
    
    }
    The error is on line 13
    :Warning: format '%lf' expects argument of type 'double', but argument 2 has type 'double (*)(double, double, double)' [-Wformat]

    Not so sure why its showing an error. I mean i changed integer 5 to 5.0.
    I declared everything as double.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You never passed your function any arguments... double fiveycost( double house, double fuel_cost, double tax_rate); look at your prototype some more. You must pass it 3 doubles.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by tmac619619 View Post
    :Warning: format '%lf' expects argument of type 'double', but argument 2 has type 'double (*)(double, double, double)' [-Wformat]
    That type, 'double (*)(double, double, double)', is a function pointer type. This is because the effect of this line:

    Code:
    printf(" The total cost is $%lf.",fiveycost);
    is not to call the fiveycost function. You need to pass arguments to fiveycost. Function calls look like

    Code:
    func() //no args
    func2(a, b) // 2 args
    Using "fiveycost" on its own like this means the type is that of a function pointer, and the value is an address. You don't need to worry about the details of that bit right now.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Could you help me out with passing arguments?

    I'm not sure what it would look like

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You just pass what you want into your function... In your case it would look like
    Code:
    printf("%lf",fiveycost(house, fuel_cost, tax_rate));
    Notice that you drop the key words "Double" with each variable passed.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Thanks it worked.

    Code:
    printf(" The total cost is $%lf.",fiveycost);
    Code:
    printf("The total cost is%lf",fiveycost(house, fuel_cost, tax_rate));
    What is the difference between those two?
    The first one doesn't work because i just presented the function fiveycost, but haven't called any arguments right?

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Yes, here is a link that should help you clear up some concepts on functions.
    Functions in C - Cprogramming.com

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Thank you.

    Once i learn this, i will never forget it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling Warning
    By Micki-Zee in forum C Programming
    Replies: 3
    Last Post: 06-29-2011, 09:40 AM
  2. Replies: 4
    Last Post: 03-12-2011, 06:59 PM
  3. Warning messages compiling C program
    By puth in forum C Programming
    Replies: 12
    Last Post: 09-09-2010, 08:57 AM
  4. Replies: 9
    Last Post: 05-28-2010, 10:11 AM
  5. Compiling warning..
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 10:45 PM