Issue with "double" argument type

This is a discussion on Issue with "double" argument type within the C Programming forums, part of the General Programming Boards category; Hi all, I'm just learning C, and I started playing around with the double argument type. So I wrote a ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Issue with "double" argument type

    Hi all,

    I'm just learning C, and I started playing around with the double argument type. So I wrote a simple print/scan/print program to see how it works. My code is below.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
            double a;
    
            printf("\nEnter number: ");
            scanf("%lf", a);
    
            printf("%f", a);
    }
    I wrote this code according to my textbook, along with instructions from my professor on how to read the variable and how to call the variable for a print statement. However, I get seemingly conflicting errors when trying to compile:

    >double_explore.c:9: warning: format ‘%lf’ expects type ‘double *’, but argument 2 has type >‘double’

    So, I edit the code so that the "double a" in my declarations becomes "double *a". Then, compiling again, I get this error:

    >double_explore.c:11: warning: format ‘%f’ expects type ‘double’, but argument 2 has type >‘double *’

    Any ideas?

    Thanks.

    -Sean

    P.S. Not sure if it matters, but in case it does, I'm running Ubuntu 10.10 and using vim to edit the code.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,437
    Leave a as type double, and
    Code:
    scanf("%lf", &a);
    Scanf requires pointers, not values.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
            scanf("%lf", &a);
    scanf() needs a pointer to the variable.


    EDIT:--------------------------

    Grumpy! Hey!

    Great minds think alike....

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I would use "%lf" for the printf too.

  5. #5
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    Quote Originally Posted by nonoob View Post
    I would use "%lf" for the printf too.
    Careful! printf("%lf", ..) in C89 is undefined; it only became legal in C99. I'm not sure how many (if any) C89 implementations don't support it, but you're guaranteed that %f will work in both C89 and C99.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by cas View Post
    Careful! printf("%lf", ..) in C89 is undefined; it only became legal in C99. I'm not sure how many (if any) C89 implementations don't support it, but you're guaranteed that %f will work in both C89 and C99.
    IBTD, "%lf" is the standard C idiom for scanning or printing a double; doesn't matter which Cx9 it is.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    IBTD, "%lf" is the standard C idiom for scanning or printing a double; doesn't matter which Cx9 it is.
    IBTD... I think you'll find that it's use before C99 is very much compiler dependent.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by CommonTater View Post
    IBTD... I think you'll find that it's use before C99 is very much compiler dependent.
    Actually, he is correct for scanf() and wrong for printf(). The scanf() set of functions do take a l modifier for doubles in C89. See 7.19.6.2 of the standard.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bithub View Post
    Actually, he is correct for scanf() and wrong for printf(). The scanf() set of functions do take a l modifier for doubles in C89. See 7.19.6.2 of the standard.
    Ok... that's interesting, thank you.

    Mostly though I've just been waiting a really long time to type "IBTD"

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by CommonTater View Post
    Ok... that's interesting, thank you.

    Mostly though I've just been waiting a really long time to type "IBTD"
    It took me about a minute of looking at his post before I figured out what it stood for. I guess I'm getting too old
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,437
    Quote Originally Posted by bithub View Post
    Actually, he is correct for scanf() and wrong for printf(). The scanf() set of functions do take a l modifier for doubles in C89. See 7.19.6.2 of the standard.
    That's because, in C89, arguments of type float are implicitly promoted to double when passed as arguments to a function (i.e. the function receives a value of type double). There is therefore no need to specify the "lf" for a float - as far as printf() is concerned it is long (i.e. double) anyway.

    scanf() does care about the size differences, as it writes to the value. The size of the type therefore matters.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  5. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21