printf("%#04x\n", 0); print 0000 not 0x00

Discussion in 'C Programming' started by baumann@pan, Jul 1, 2005.

  1. baumann@pan

    baumann@pan Guest

    hi all,

    i hope
    printf("%#04x\n", 0);
    will output 0x00,
    but visual c++ studio 6 outputs 0000.

    how can i get 0x00?

    thanks
     
    baumann@pan, Jul 1, 2005
    #1
    1. Advertising

  2. baumann@pan

    Ganesh babu Guest

    printf("0x%#02x", 0);

    baumann@pan wrote:
    > hi all,
    >
    > i hope
    > printf("%#04x\n", 0);
    > will output 0x00,
    > but visual c++ studio 6 outputs 0000.
    >
    > how can i get 0x00?
    >
    > thanks
     
    Ganesh babu, Jul 1, 2005
    #2
    1. Advertising

  3. baumann@pan

    Jack Klein Guest

    On 30 Jun 2005 18:44:21 -0700, "baumann@pan" <>
    wrote in comp.lang.c:

    > hi all,
    >
    > i hope
    > printf("%#04x\n", 0);
    > will output 0x00,
    > but visual c++ studio 6 outputs 0000.
    >
    > how can i get 0x00?
    >
    > thanks


    By coding:

    printf("0x%04x\n", 0U);

    There is no way to get printf() to do what you want with the format
    string you are using and a value of 0, since it obviously does not
    want to. Here is what the C standard says about the '#' flag:

    "For x (or X) conversion, a nonzero result has 0x (or 0X) prefixed to
    it."

    That does not prevent an implementation from putting 0x in front of
    the output for a value of 0, but it also most certainly does not
    require it to do so.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 1, 2005
    #3
  4. Jack Klein wrote:
    > "baumann@pan" <> wrote in comp.lang.c:
    > > hi all,
    > >
    > > i hope
    > > printf("%#04x\n", 0);
    > > will output 0x00,
    > > but visual c++ studio 6 outputs 0000.
    > >
    > > how can i get 0x00?
    > >
    > > thanks

    >
    > By coding:
    >
    > printf("0x%04x\n", 0U);


    ITYM: printf("0x%02x\n", 0U);

    > <snip>


    --
    Peter
     
    Peter Nilsson, Jul 1, 2005
    #4
  5. baumann@pan

    Jack Klein Guest

    On 30 Jun 2005 19:35:19 -0700, "Ganesh babu" <>
    wrote in comp.lang.c:

    > printf("0x%#02x", 0);
    >
    > baumann@pan wrote:
    > > hi all,
    > >
    > > i hope
    > > printf("%#04x\n", 0);
    > > will output 0x00,
    > > but visual c++ studio 6 outputs 0000.
    > >
    > > how can i get 0x00?
    > >
    > > thanks


    ....but note that the wording of the standard does not prohibit an
    implementation from prepending a "0x" even if the value is 0. So on
    some implementations you might get "0x0x00".

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 1, 2005
    #5
  6. baumann@pan

    Jack Klein Guest

    On 30 Jun 2005 20:00:50 -0700, "Peter Nilsson" <>
    wrote in comp.lang.c:

    > Jack Klein wrote:
    > > "baumann@pan" <> wrote in comp.lang.c:
    > > > hi all,
    > > >
    > > > i hope
    > > > printf("%#04x\n", 0);
    > > > will output 0x00,
    > > > but visual c++ studio 6 outputs 0000.
    > > >
    > > > how can i get 0x00?
    > > >
    > > > thanks

    > >
    > > By coding:
    > >
    > > printf("0x%04x\n", 0U);

    >
    > ITYM: printf("0x%02x\n", 0U);


    You are correct, sir. I misinterpreted the width of 4 in the OP's
    original. Thanks.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 1, 2005
    #6
  7. On Thu, 30 Jun 2005 22:58:29 -0500, Jack Klein wrote:

    > On 30 Jun 2005 19:35:19 -0700, "Ganesh babu" <>
    > wrote in comp.lang.c:
    >
    >> printf("0x%#02x", 0);


    Which will output for example 0x0x1 for an argument value of 1. If you
    were only interested in 0 values you could write simply:

    printf("0x00");

    >> baumann@pan wrote:
    >> > hi all,
    >> >
    >> > i hope
    >> > printf("%#04x\n", 0);
    >> > will output 0x00,
    >> > but visual c++ studio 6 outputs 0000.
    >> >
    >> > how can i get 0x00?
    >> >
    >> > thanks

    >
    > ...but note that the wording of the standard does not prohibit an
    > implementation from prepending a "0x" even if the value is 0.


    I can't see anything in the standard that permits it to prepend 0x for a 0
    value.

    > So on
    > some implementations you might get "0x0x00".


    The # flag specifies an alterative form. It is described in terms of how
    it differs from the primary form. For a 0 value there is no difference
    specified. You seem to be saying that the output for a 0 value is not
    specified. If that is true the output could be anything at all which would
    make %#x useless for outputting 0 values.

    Lawrence
     
    Lawrence Kirby, Jul 1, 2005
    #7
  8. baumann@pan

    Jack Klein Guest

    On Fri, 01 Jul 2005 12:12:11 +0100, Lawrence Kirby
    <> wrote in comp.lang.c:

    > On Thu, 30 Jun 2005 22:58:29 -0500, Jack Klein wrote:
    >
    > > On 30 Jun 2005 19:35:19 -0700, "Ganesh babu" <>
    > > wrote in comp.lang.c:
    > >
    > >> printf("0x%#02x", 0);

    >
    > Which will output for example 0x0x1 for an argument value of 1. If you
    > were only interested in 0 values you could write simply:
    >
    > printf("0x00");
    >
    > >> baumann@pan wrote:
    > >> > hi all,
    > >> >
    > >> > i hope
    > >> > printf("%#04x\n", 0);
    > >> > will output 0x00,
    > >> > but visual c++ studio 6 outputs 0000.
    > >> >
    > >> > how can i get 0x00?
    > >> >
    > >> > thanks

    > >
    > > ...but note that the wording of the standard does not prohibit an
    > > implementation from prepending a "0x" even if the value is 0.

    >
    > I can't see anything in the standard that permits it to prepend 0x for a 0
    > value.


    I don't see anything that forbids it. It says "For x (or X)
    conversion, a nonzero result has 0x (or 0X) prefixed to it." It does
    not say anything at all about a result that is not nonzero.

    > > So on
    > > some implementations you might get "0x0x00".

    >
    > The # flag specifies an alterative form. It is described in terms of how
    > it differs from the primary form. For a 0 value there is no difference
    > specified. You seem to be saying that the output for a 0 value is not
    > specified. If that is true the output could be anything at all which would
    > make %#x useless for outputting 0 values.
    >
    > Lawrence


    To me it looks like one of those cases where the wording in the
    standard leaves a loop hole. Typical standardese, as one is likely to
    see if one brings up such issues on comp.std.c, is that since the
    standard specifically defines behavior for the nonzero case and does
    not define behavior for the 0 case, the 0 case is undefined.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jul 2, 2005
    #8
  9. On Fri, 01 Jul 2005 22:18:49 -0500, Jack Klein wrote:

    >> The # flag specifies an alterative form. It is described in terms of how
    >> it differs from the primary form. For a 0 value there is no difference
    >> specified. You seem to be saying that the output for a 0 value is not
    >> specified. If that is true the output could be anything at all which would
    >> make %#x useless for outputting 0 values.
    >>
    >> Lawrence

    >
    > To me it looks like one of those cases where the wording in the
    > standard leaves a loop hole. Typical standardese, as one is likely to
    > see if one brings up such issues on comp.std.c, is that since the
    > standard specifically defines behavior for the nonzero case and does
    > not define behavior for the 0 case, the 0 case is undefined.


    I agree, the wording could be improved.

    Lawrence
     
    Lawrence Kirby, Jul 4, 2005
    #9
    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. =?Utf-8?B?SlJPQ2FtYXJv?=
    Replies:
    6
    Views:
    4,250
  2. Replies:
    0
    Views:
    10,284
  3. Joseph Suprenant

    printing 0x00 in ascii

    Joseph Suprenant, Nov 20, 2003, in forum: C++
    Replies:
    3
    Views:
    796
    Noah Roberts
    Nov 20, 2003
  4. =?Utf-8?B?V2VlIEt1YW4=?=
    Replies:
    0
    Views:
    3,071
    =?Utf-8?B?V2VlIEt1YW4=?=
    Nov 20, 2006
  5. Tomás

    Zero always == 0000 0000

    Tomás, Mar 5, 2006, in forum: C++
    Replies:
    15
    Views:
    576
    Tomás
    Mar 8, 2006
Loading...

Share This Page