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

B

baumann@pan

hi all,

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

how can i get 0x00?

thanks
 
J

Jack Klein

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.
 
J

Jack Klein

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

....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".
 
L

Lawrence Kirby

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");
...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
 
J

Jack Klein

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");


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.
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.
 
L

Lawrence Kirby

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top