Double printf format...

Discussion in 'C Programming' started by naunetr, Mar 13, 2008.

  1. naunetr

    naunetr Guest

    in program below

    #include <stdio.h>
    int main()
    {
    int id = 123, io = 0123, ih = 0x123;
    long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
    float f = 123.456f;
    double d = 01234.56789L;

    printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
    id, id, id, io, io, io, ih, ih, ih);
    printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh = %ld\t%lo\t%l\n",
    ld, ld, ld, lo, lo, lo, lh, lh, lh);
    printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d, d);
    return 0;
    }

    the output got is:
    id = 123 173 7b
    io = 83 123 53
    ih = 291 %O 123
    ld = 1234567 4553207 12d687
    lo = 342391 1234567 53977
    lh = 19088743 110642547 1234567
    f = 123.456001 1.234560e+02 123.456
    d = 1234.567890 1.234568E+03 1234.57

    why is there a %O in line 3 instead of octal o/p expected?
    and in line for d the last o/p is 1234.57. but %g is either %lf or %le
    right? so if it is %lf then why is it 1234.57 instead of 1234.56789?

    thanks
    naunetr, Mar 13, 2008
    #1
    1. Advertising

  2. naunetr

    santosh Guest

    naunetr wrote:

    > in program below
    >
    > #include <stdio.h>
    > int main()
    > {
    > int id = 123, io = 0123, ih = 0x123;
    > long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
    > float f = 123.456f;
    > double d = 01234.56789L;
    >
    > printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
    > id, id, id, io, io, io, ih, ih, ih);
    > printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh =
    > %ld\t%lo\t%l\n",
    > ld, ld, ld, lo, lo, lo, lh, lh, lh);
    > printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d,
    > d); return 0;
    > }
    >
    > the output got is:
    > id = 123 173 7b
    > io = 83 123 53
    > ih = 291 %O 123
    > ld = 1234567 4553207 12d687
    > lo = 342391 1234567 53977
    > lh = 19088743 110642547 1234567
    > f = 123.456001 1.234560e+02 123.456
    > d = 1234.567890 1.234568E+03 1234.57
    >
    > why is there a %O in line 3 instead of octal o/p expected?


    The format specifier for octal output is %o, not %O. In C case is
    important.

    > and in line for d the last o/p is 1234.57. but %g is either %lf or %le
    > right? so if it is %lf then why is it 1234.57 instead of 1234.56789?


    I think that's a precision issue.
    santosh, Mar 13, 2008
    #2
    1. Advertising

  3. naunetr

    Lew Pitcher Guest

    Post reordered to provide answer to question

    naunetr wrote:
    > why is there a %O in line 3 instead of octal o/p expected?


    Because you put it there. The relevant fragment of the format string is
    ih = %d\t%O\t%X\n

    Notice the %O in the string above? There is no %O conversion specifier, and
    the "octal" conversion specifier is %o

    > and in line for d the last o/p is 1234.57. but %g is either %lf or %le
    > right?


    Yes and no. %g (in your case %lG) is equivalent to either %f or %e; the l
    length modifier has no effect on %e, %E, %f, %F, %g, or %G conversion
    specifiers.

    For the %g conversion, the %e "style" is used only if the exponent is less
    than -4 (which, for your value, it is not), or is greater than or equal to
    the precision (which, for your value, it is not, because of the behaviour I
    remark on below). So, for your values, your %lG "defaults" to
    the %f "style".

    > so if it is %lf then why is it 1234.57 instead of 1234.56789?


    Both %e and %f have specific behaviours depending on the supplied precision,
    including specific behaviours when the precision is not supplied.
    Fortunatly, the %e behaviour and the %f behaviour for an unspecified
    precision is exactly the same: "if the precision is missing, it is taken as
    6". Thus %g (in your case %lG) provides 6 digits of precision, and (given a
    value of 1234.56789) will produce a printed result (to 6 digits precision)
    of 1234.57 (.56789 rounds up to .57).

    > in program below
    >
    > #include <stdio.h>
    > int main()
    > {
    > int id = 123, io = 0123, ih = 0x123;
    > long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
    > float f = 123.456f;
    > double d = 01234.56789L;
    >
    > printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
    > id, id, id, io, io, io, ih, ih, ih);
    > printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh = %ld\t%lo\t%l\n",
    > ld, ld, ld, lo, lo, lo, lh, lh, lh);
    > printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d, d);
    > return 0;
    > }
    >
    > the output got is:
    > id = 123 173 7b
    > io = 83 123 53
    > ih = 291 %O 123
    > ld = 1234567 4553207 12d687
    > lo = 342391 1234567 53977
    > lh = 19088743 110642547 1234567
    > f = 123.456001 1.234560e+02 123.456
    > d = 1234.567890 1.234568E+03 1234.57


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Mar 13, 2008
    #3
  4. naunetr

    naunetr Guest

    On Thu, 13 Mar 2008 13:13:48 -0400, Lew Pitcher <>
    wrote:

    On Thu, 13 Mar 2008 13:13:48 -0400, Lew Pitcher <>
    wrote:

    > Post reordered to provide answer to question
    >
    > naunetr wrote:
    >> why is there a %O in line 3 instead of octal o/p expected?

    >
    > Because you put it there. The relevant fragment of the format string is
    > ih = %d\t%O\t%X\n
    >
    > Notice the %O in the string above? There is no %O conversion specifier,
    > and the "octal" conversion specifier is %o


    thanks Lew. is there a specifier which will put a 0 in front off the octal
    number? like 0x for %#x?

    >> and in line for d the last o/p is 1234.57. but %g is either %lf or %le
    >> right?

    >
    > Yes and no. %g (in your case %lG) is equivalent to either %f or %e; the
    > l length modifier has no effect on %e, %E, %f, %F, %g, or %G conversion
    > specifiers.


    sorry got it confused with scanf! but why is there a %F too? shouldn't %f
    be enough?

    > For the %g conversion, the %e "style" is used only if the exponent is
    > less than -4 (which, for your value, it is not), or is greater than or
    > equal to the precision (which, for your value, it is not, because of the
    > behaviour I remark on below). So, for your values, your %lG "defaults"
    > to the %f "style".
    >
    >> so if it is %lf then why is it 1234.57 instead of 1234.56789?

    >
    > Both %e and %f have specific behaviours depending on the supplied
    > precision, including specific behaviours when the precision is not
    > supplied. Fortunatly, the %e behaviour and the %f behaviour for an
    > unspecified precision is exactly the same: "if the precision is missing,
    > it is taken as 6". Thus %g (in your case %lG) provides 6 digits of
    > precision, and (given a value of 1234.56789) will produce a printed
    > result (to 6 digits precision) of 1234.57 (.56789 rounds up to .57).


    sorry but i'm still confused. in this case then why doesn't the first o/p
    on the last line not also print 1234.57 instead of 1234.56789?

    anyway thanx a lot.

    >> in program below
    >>
    >> #include <stdio.h>
    >> int main()
    >> {
    >> int id = 123, io = 0123, ih = 0x123;
    >> long ld = 1234567L, lo = 01234567l, lh = 0X1234567L; float f =
    >> 123.456f;
    >> double d = 01234.56789L;
    >>
    >> printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
    >> id, id, id, io, io, io, ih, ih, ih);
    >> printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh =
    >> %ld\t%lo\t%l\n",
    >> ld, ld, ld, lo, lo, lo, lh, lh, lh);
    >> printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d, d);
    >> return 0;
    >> }
    >>
    >> the output got is:
    >> id = 123 173 7b
    >> io = 83 123 53
    >> ih = 291 %O 123
    >> ld = 1234567 4553207 12d687
    >> lo = 342391 1234567 53977
    >> lh = 19088743 110642547 1234567 f = 123.456001 1.234560e+02
    >> 123.456 d = 1234.567890 1.234568E+03 1234.57
    naunetr, Mar 13, 2008
    #4
  5. naunetr wrote:
    [...]
    > >> why is there a %O in line 3 instead of octal o/p expected?

    > >
    > > Because you put it there. The relevant fragment of the format string is
    > > ih = %d\t%O\t%X\n
    > >
    > > Notice the %O in the string above? There is no %O conversion specifier,
    > > and the "octal" conversion specifier is %o

    >
    > thanks Lew. is there a specifier which will put a 0 in front off the octal
    > number? like 0x for %#x?


    What about "0%o" and "0x%x"?

    [...]

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 13, 2008
    #5
  6. In article <frbq1f$ocq$>,
    naunetr <> wrote:

    >thanks Lew. is there a specifier which will put a 0 in front off the octal
    >number? like 0x for %#x?


    %#o

    C89 4.9.6.1 The fprintf Function

    # The result is to be converted to an "alternate form." For o
    conversion, it increases the precision to force the first digit
    of the result to be a zero. For x (or X) conversion, a
    nonzero result will have 0x (or 0X) prefixed to it. [...]


    >sorry got it confused with scanf! but why is there a %F too? shouldn't %f
    >be enough?


    C89 does not have %F (but does have %E and %G). %E and %G use a
    capital E for the exponent signifier instead of a lower-case e
    but are otherwise the same as %e and %g respectively.
    --
    "The quirks and arbitrariness we observe force us to the
    conclusion that ours is not the only universe." -- Walter Kistler
    Walter Roberson, Mar 13, 2008
    #6
  7. naunetr

    naunetr Guest

    On Thu, 13 Mar 2008 19:16:27 +0000, -cnrc.gc.ca (Walter
    Roberson) wrote:

    On Thu, 13 Mar 2008 19:16:27 +0000, -cnrc.gc.ca (Walter
    Roberson) wrote:

    > In article <frbq1f$ocq$>, naunetr
    > <> wrote:
    >
    >>thanks Lew. is there a specifier which will put a 0 in front off the
    >>octal number? like 0x for %#x?

    >
    > %#o


    oops! thought that works only for hex specifier %x.

    > C89 4.9.6.1 The fprintf Function
    >
    > # The result is to be converted to an "alternate form." For o
    > conversion, it increases the precision to force the first digit of
    > the result to be a zero. For x (or X) conversion, a nonzero result
    > will have 0x (or 0X) prefixed to it. [...]
    >
    >
    >>sorry got it confused with scanf! but why is there a %F too? shouldn't
    >>%f be enough?

    >
    > C89 does not have %F (but does have %E and %G). %E and %G use a capital
    > E for the exponent signifier instead of a lower-case e but are otherwise
    > the same as %e and %g respectively.


    thanks Walter. by "C89" you mean ANSI C yes?
    naunetr, Mar 14, 2008
    #7
  8. naunetr said:

    > On Thu, 13 Mar 2008 19:16:27 +0000, -cnrc.gc.ca (Walter
    > Roberson) wrote:


    <snip>

    >> C89 does not have %F (but does have %E and %G). %E and %G use a capital
    >> E for the exponent signifier instead of a lower-case e but are otherwise
    >> the same as %e and %g respectively.

    >
    > thanks Walter. by "C89" you mean ANSI C yes?


    Almost certainly it's the other way around - that is, by "ANSI C" you mean
    C89.

    Quick history for you:

    dawnatime: R invents C.
    dawnatime + a bit: K says "that's cool, let's do a book on it".
    1978: K&R published.
    1980s: ANSI undertake a standardisation process which culminates in:
    1988: K&R2 (based on draft-proposed ANSI C) published.
    1989: ANSI formally standardises C (this is C89, which is what you've got)
    1990: ISO adopts the ANSI C Standard internationally (C90)
    1999: ISO formally re-standardises C (C99)
    2000: ANSI adopts C99
    1999-2008: Almost the entire universe ignores C99 completely.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 14, 2008
    #8
  9. Richard Heathfield <> writes:

    > Quick history for you:

    <snip history>
    > 1999-2008: Almost the entire universe ignores C99 completely.


    This reflects your personal opinion more accurately than it describes
    the truth. It *is* true, of course, but uninterestingly true since
    "almost the entire universe ignores X completely" is true for a very
    large set of Xs -- including C90!

    --
    Ben.
    Ben Bacarisse, Mar 14, 2008
    #9
  10. Ben Bacarisse said:

    > Richard Heathfield <> writes:
    >
    >> Quick history for you:

    > <snip history>
    >> 1999-2008: Almost the entire universe ignores C99 completely.

    >
    > This reflects your personal opinion more accurately than it describes
    > the truth.


    Well, actually it reflects my personal understanding of the truth. The only
    validated C99 compilers I know about are:

    EDG (Pentium PIII, Linux 7.x series)
    IBM XL (IBM POWER PC_970, AIX 5.3h)
    IBM Visual Age (IBM PowerPC systems w/ CHRP system architecture, AIX 5L for
    POWER, Version 5.2)
    LMPCC C99 Compiler for Linux / PowerPC (Motorola PowerPC MPC7455, Terrasoft
    Solutions, Yellow Dog Linux 3.0)
    Sun Studio 9 (SPARC Ultra/Intel x86/AMD 64, Solaris 10)

    (Some of these depend on the validated C99-conforming Dinkum Unabridged
    Library.)

    Data source: http://www.peren.com/pages/cvsa_isocvpl.htm


    I don't have time to research into how many C90-conforming compilers there
    are, but ISTR that there are a hundred or so. Does anyone have any better
    data?


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 14, 2008
    #10
  11. In article <>,
    Richard Heathfield <> wrote:

    >>> 1999-2008: Almost the entire universe ignores C99 completely.


    >> This reflects your personal opinion more accurately than it describes
    >> the truth.


    >Well, actually it reflects my personal understanding of the truth. The only
    >validated C99 compilers I know about are:


    The absence of validated C99 compilers doesn't mean that the universe
    is completely ignoring C99. For example, I can use inline functions
    or VLAs with fair confidence that they will be available, and
    sufficiently compatible, on all the systems I care about. I don't
    think this would be true without the existence of C99.

    -- Richard

    --
    :wq
    Richard Tobin, Mar 14, 2008
    #11
  12. Richard Tobin said:

    <snip>

    > The absence of validated C99 compilers doesn't mean that the universe
    > is completely ignoring C99. For example, I can use inline functions
    > or VLAs with fair confidence that they will be available, and
    > sufficiently compatible, on all the systems I care about. I don't
    > think this would be true without the existence of C99.


    Understood, but the difficulty is in identifying which features of C99 can
    be used "portably". We sure could use a central reference, listing C99
    features down the left, compilers across the top, and ticks in the boxes
    where the feature has been implemented according to C99 semantics. That
    way, maybe people could start to use C99 features with some degree of
    confidence that they will be portable to "all the systems I care about",
    for various values of "I".

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Mar 14, 2008
    #12
  13. naunetr

    Micah Cowan Guest

    (Richard Tobin) writes:

    > In article <>,
    > Richard Heathfield <> wrote:
    >
    >>>> 1999-2008: Almost the entire universe ignores C99 completely.

    >
    >>> This reflects your personal opinion more accurately than it describes
    >>> the truth.

    >
    >>Well, actually it reflects my personal understanding of the truth. The only
    >>validated C99 compilers I know about are:

    >
    > The absence of validated C99 compilers doesn't mean that the universe
    > is completely ignoring C99. For example, I can use inline functions
    > or VLAs with fair confidence that they will be available, and
    > sufficiently compatible, on all the systems I care about. I don't
    > think this would be true without the existence of C99.


    I think it would be true regardless. They existed relatively widely
    before C99, AFAICT (with "inline"'s semantics usually being based on
    C++'s rules).

    --
    Just my 2¢, of course
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
    Micah Cowan, Mar 14, 2008
    #13
  14. naunetr

    CBFalconer Guest

    Standards use (was: Double printf format...)

    Richard Heathfield wrote:
    > Richard Tobin said:
    >
    > <snip>
    >
    >> The absence of validated C99 compilers doesn't mean that the
    >> universe is completely ignoring C99. For example, I can use
    >> inline functions or VLAs with fair confidence that they will
    >> be available, and sufficiently compatible, on all the systems
    >> I care about. I don't think this would be true without the
    >> existence of C99.

    >
    > Understood, but the difficulty is in identifying which features
    > of C99 can be used "portably". We sure could use a central
    > reference, listing C99 features down the left, compilers across
    > the top, and ticks in the boxes where the feature has been
    > implemented according to C99 semantics. That way, maybe people
    > could start to use C99 features with some degree of confidence
    > that they will be portable to "all the systems I care about",
    > for various values of "I".


    There are two primary areas in which standards are useful. One is
    in specifying what is needed to process some source code. Another
    is in specifying what features source code can use.

    For the first, there is no loss if the source is prepared without
    using all features of the standard. The result remains perfectly
    portable.

    For the second, things are more complicated. If the writer wants
    to maximize portability he can write to the C90 standard, as you
    do. This requires checking features used against that standard,
    and things can slip through. More often the writer uses the C99
    standard for checking, and then eliminates things he knows are not
    available in C90. Errors can abound.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 14, 2008
    #14
  15. naunetr

    Guest

    naunetr <> wrote:
    >
    > sorry got it confused with scanf! but why is there a %F too? shouldn't %f
    > be enough?


    %F was new in C99. It differs from %f in the treatment of exceptional
    values: %F produces INF, INFINITY, or NAN whereas %f produces inf,
    infinity, or nan.

    -Larry Jones

    In a minute, you and I are going to settle this out of doors. -- Calvin
    , Mar 14, 2008
    #15
    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,450
    Victor Bazarov
    Feb 17, 2005
  2. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

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

    printf format for double

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

    format for double for printf

    RoS, Dec 3, 2007, in forum: C Programming
    Replies:
    6
    Views:
    614
    Walter Roberson
    Dec 3, 2007
Loading...

Share This Page