Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf("%#04x\n", 0); print 0000 not 0x00

Reply
Thread Tools

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

 
 
baumann@pan
Guest
Posts: n/a
 
      07-01-2005
hi all,

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

how can i get 0x00?

thanks

 
Reply With Quote
 
 
 
 
Ganesh babu
Guest
Posts: n/a
 
      07-01-2005
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


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      07-01-2005
On 30 Jun 2005 18:44:21 -0700, "baumann@pan" <(E-Mail Removed)>
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/~a...FAQ-acllc.html
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-01-2005
Jack Klein wrote:
> "baumann@pan" <(E-Mail Removed)> 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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-01-2005
On 30 Jun 2005 19:35:19 -0700, "Ganesh babu" <(E-Mail Removed)>
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/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-01-2005
On 30 Jun 2005 20:00:50 -0700, "Peter Nilsson" <(E-Mail Removed)>
wrote in comp.lang.c:

> Jack Klein wrote:
> > "baumann@pan" <(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-01-2005
On Thu, 30 Jun 2005 22:58:29 -0500, Jack Klein wrote:

> On 30 Jun 2005 19:35:19 -0700, "Ganesh babu" <(E-Mail Removed)>
> 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
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      07-02-2005
On Fri, 01 Jul 2005 12:12:11 +0100, Lawrence Kirby
<(E-Mail Removed)> 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" <(E-Mail Removed)>
> > 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/~a...FAQ-acllc.html
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      07-04-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can an MD5 sum be 0000-0000-0000-0000? Matt Thomas Computer Security 2 01-10-2010 11:23 PM
CLSID {00024500-0000-0000-C000-000000000046} failed due to: 800800 =?Utf-8?B?V2VlIEt1YW4=?= ASP .Net 0 11-20-2006 03:51 AM
Zero always == 0000 0000 Tomás C++ 15 03-08-2006 08:33 PM
Access denied attempting to launch a DCOM Server. The server is: {000C101C-0000-0000-C000-000000000046} The user is ASPNET/ServerName, SID=S-1-5-21-1606980848-602162358-1801674531-1007. c.verma@gmail.com ASP .Net 0 05-26-2005 08:44 PM
COM object with CLSID {00024500-0000-0000-C000-000000000046} is either not valid or not registered. =?Utf-8?B?SlJPQ2FtYXJv?= ASP .Net 6 04-07-2004 01:48 PM



Advertisments