format for double for printf

Discussion in 'C Programming' started by RoS, Dec 3, 2007.

  1. RoS

    RoS Guest

    the format for conversion of double in the function printf is
    "%f" or "%lf" or both?

    for example
    printf("%f", (double)0.0);
    printf("%lf", (double)0.0);
    are both ok?

    Good morning
    Thank you
    RoS, Dec 3, 2007
    #1
    1. Advertising

  2. it seems like that in c there is no need to print a double varible
    with %lf format
    %f is ok
    but for long interger , u should use %ld

    RoS wrote:
    > the format for conversion of double in the function printf is
    > "%f" or "%lf" or both?
    >
    > for example
    > printf("%f", (double)0.0);
    > printf("%lf", (double)0.0);
    > are both ok?
    >
    > Good morning
    > Thank you
    Thomas X. Iverson, Dec 3, 2007
    #2
    1. Advertising

  3. RoS

    Ian Collins Guest

    RoS wrote:
    > the format for conversion of double in the function printf is
    > "%f" or "%lf" or both?
    >
    > for example
    > printf("%f", (double)0.0);
    > printf("%lf", (double)0.0);
    > are both ok?
    >

    Yes they are equivalent, the l modifier has no effect for the f specifier.

    --
    Ian Collins.
    Ian Collins, Dec 3, 2007
    #3
  4. RoS

    pete Guest

    Ian Collins wrote:
    >
    > RoS wrote:
    > > the format for conversion of double in the function printf is
    > > "%f" or "%lf" or both?
    > >
    > > for example
    > > printf("%f", (double)0.0);
    > > printf("%lf", (double)0.0);
    > > are both ok?
    > >

    > Yes they are equivalent,
    > the l modifier has no effect for the f specifier.


    That's the case for C99.
    For C89,
    a function call to printf is undefined
    if "%lf" is the first argument.

    --
    pete
    pete, Dec 3, 2007
    #4
  5. RoS

    Guest

    On Dec 3, 3:49 pm, pete <> wrote:
    > Ian Collins wrote:
    >
    > > RoS wrote:
    > > > the format for conversion of double in the function printf is
    > > > "%f" or "%lf" or both?

    >
    > > > for example
    > > > printf("%f", (double)0.0);
    > > > printf("%lf", (double)0.0);
    > > > are both ok?

    >
    > > Yes they are equivalent,
    > > the l modifier has no effect for the f specifier.

    >
    > That's the case for C99.
    > For C89,
    > a function call to printf is undefined
    > if "%lf" is the first argument.


    The first? "%d %lf" is not undefined?
    , Dec 3, 2007
    #5
  6. writes:
    > On Dec 3, 3:49 pm, pete <> wrote:
    >> Ian Collins wrote:
    >>
    >> > RoS wrote:
    >> > > the format for conversion of double in the function printf is
    >> > > "%f" or "%lf" or both?

    >>
    >> > > for example
    >> > > printf("%f", (double)0.0);
    >> > > printf("%lf", (double)0.0);
    >> > > are both ok?

    >>
    >> > Yes they are equivalent,
    >> > the l modifier has no effect for the f specifier.

    >>
    >> That's the case for C99.
    >> For C89,
    >> a function call to printf is undefined
    >> if "%lf" is the first argument.

    >
    > The first? "%d %lf" is not undefined?


    Yes, "%d %lf" is undefined for C89/C90's printf. He said "if", not
    "if and only if".

    (Incidentally, given printf("%d %lf", x, y), the first *argument*
    isn't %d; it's the string "%d %ld".)

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 3, 2007
    #6
  7. In article <>,
    pete <> wrote:
    >Ian Collins wrote:
    >>
    >> RoS wrote:
    >> > the format for conversion of double in the function printf is
    >> > "%f" or "%lf" or both?


    >> Yes they are equivalent,
    >> the l modifier has no effect for the f specifier.


    >That's the case for C99.
    >For C89,
    >a function call to printf is undefined
    >if "%lf" is the first argument.


    And just to confuse matters, in C89, "%Lf" *is* defined and
    indicates that the corresponding argument is long double .
    --
    "There are some ideas so wrong that only a very intelligent person
    could believe in them." -- George Orwell
    Walter Roberson, Dec 3, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,473
    Victor Bazarov
    Feb 17, 2005
  2. ben
    Replies:
    4
    Views:
    611
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,237
  4. Joachim Schmitz

    printf format for double

    Joachim Schmitz, Oct 2, 2007, in forum: C Programming
    Replies:
    1
    Views:
    482
    Ben Pfaff
    Oct 2, 2007
  5. naunetr

    Double printf format...

    naunetr, Mar 13, 2008, in forum: C Programming
    Replies:
    14
    Views:
    11,887
Loading...

Share This Page