By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

'#' conversion flag in printf() doesn't work with NULL character

P: n/a
L.S.

I've observed unexpected behaviour regarding the usage of the '#' flag
in the conversion specification in the printf() family of functions. Did
I detect a bug, or is there something wrong with my expectations
regarding the effect of the following code:

printf("NULL as hex: %#4.2x\n", '\0');
Expected output
===============

NULL as hex: 0x00

Observed output (white space is significant)
===============

NULL as hex: 00

Thanks in advance for any comments,

Erik Leunissen
==============

--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <42*********************@reader20.nntp.hccnet.nl >,
Erik Leunissen <lo**@the.footer.invalid> wrote:
:I've observed unexpected behaviour regarding the usage of the '#' flag
:in the conversion specification in the printf() family of functions.

: printf("NULL as hex: %#4.2x\n", '\0');

:Expected output
:NULL as hex: 0x00
:Observed output (white space is significant)
:NULL as hex: 00

The C89 standard says that # indicates that an alternate output form
should be used, and that for hex, non-zero values will have 0x or 0X
prepended. You happen to be outputing the zero value, so you have
fallen into behaviour that the standard does not nail down. The
C89 standard doesn't say that 0x cannot be prepending for the zero value,
but it doesn't require it either.
--
Reviewers should be required to produce a certain number of
negative reviews - like police given quotas for handing out
speeding tickets. -- The Audio Anarchist
Nov 14 '05 #2

P: n/a
Thanks for providing clarity.

Apart from that clarity, does anybody know the reason why the C89
standard makes an exception in (deliberately?) not specifying the
behaviour for the NULL character?
TIA,

Erik Leunissen
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Nov 14 '05 #3

P: n/a
In article <42*********************@reader1.nntp.hccnet.nl> ,
Erik Leunissen <lo**@the.footer.invalid> wrote:
:Apart from that clarity, does anybody know the reason why the C89
:standard makes an exception in (deliberately?) not specifying the
:behaviour for the NULL character?

To be pedantic, it doesn't make any exception for the NULL character.
You are using a hex formatter, not a character formatter, so if you
happen to passed a char as the value, it is going to undergo
The Usual Integral Promotions. There's nothing special about the
NULL character: it's just another value which happens to be 0.

I have no ideas why the alternative representation for 0
has no 0x for the hex formatter. Possibly because it isn't needed.
0 remains the same in any base, but leading 0's before a value
can indicate octal to strtol.
--
IEA408I: GETMAIN cannot provide buffer for WATLIB.
Nov 14 '05 #4

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <42*********************@reader1.nntp.hccnet.nl> ,
Erik Leunissen <lo**@the.footer.invalid> wrote:
:Apart from that clarity, does anybody know the reason why the C89
:standard makes an exception in (deliberately?) not specifying the
:behaviour for the NULL character?

To be pedantic, it doesn't make any exception for the NULL character.


To be pedantic, there is no NULL character. NULL is a null
pointer constant. NUL is an ASCII code point.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Nov 14 '05 #5

P: n/a
On 23 Feb 2005 19:59:33 GMT,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:

In article <42*********************@reader20.nntp.hccnet.nl >,
Erik Leunissen <lo**@the.footer.invalid> wrote:
:I've observed unexpected behaviour regarding the usage of the '#' flag
:in the conversion specification in the printf() family of functions.

: printf("NULL as hex: %#4.2x\n", '\0');

:Expected output
:NULL as hex: 0x00
:Observed output (white space is significant)
:NULL as hex: 00

The C89 standard says that # indicates that an alternate output form
should be used, and that for hex, non-zero values will have 0x or 0X
prepended. You happen to be outputing the zero value, so you have
fallen into behaviour that the standard does not nail down. The
C89 standard doesn't say that 0x cannot be prepending for the zero value,
but it doesn't require it either.


So I usualy use the format 0x%02x if I want that result. Anyway, for the
value 0 or '\0' there are no ambiguity in enterpreting the printed data.

Villy
Nov 14 '05 #6

P: n/a


Villy Kruse wrote:
On 23 Feb 2005 19:59:33 GMT,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <42*********************@reader20.nntp.hccnet.nl >,
Erik Leunissen <lo**@the.footer.invalid> wrote:
:I've observed unexpected behaviour regarding the usage of the '#' flag
:in the conversion specification in the printf() family of functions.

: printf("NULL as hex: %#4.2x\n", '\0');

:Expected output
:NULL as hex: 0x00
:Observed output (white space is significant)
:NULL as hex: 00

The C89 standard says that # indicates that an alternate output form
should be used, and that for hex, non-zero values will have 0x or 0X
prepended. You happen to be outputing the zero value, so you have
fallen into behaviour that the standard does not nail down. The
C89 standard doesn't say that 0x cannot be prepending for the zero value,
but it doesn't require it either.

So I usualy use the format 0x%02x if I want that result. Anyway, for the
value 0 or '\0' there are no ambiguity in enterpreting the printed data.


Well, base 1 numbers certainly are unpopular, even though
they would greatly simplify the arithmetic stuff ;-)
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #7

P: n/a
On Wed, 23 Feb 2005 21:38:26 +0100, Erik Leunissen wrote:
Thanks for providing clarity.

Apart from that clarity, does anybody know the reason why the C89
standard makes an exception in (deliberately?) not specifying the
behaviour for the NULL character?


If you mean converting a zero value then there's a simple reason. A zero
value is unambiguous in any base (integer base >= 2 anyway). The output
format generated by # might be useful for some purposes and can't be
otherwise generated in a simple printf() call. OTOH if you want 0x or 0X
to be output in all cases that can be done trivially by putting 0x or 0X
explicitly in the format string. So there's little point in making a
special modifier to do that.

Lawrence
Nov 14 '05 #8

P: n/a
On Wed, 23 Feb 2005 19:59:33 +0000, Walter Roberson wrote:

....
The C89 standard says that # indicates that an alternate output form
should be used, and that for hex, non-zero values will have 0x or 0X
prepended. You happen to be outputing the zero value, so you have
fallen into behaviour that the standard does not nail down. The
C89 standard doesn't say that 0x cannot be prepending for the zero value,
but it doesn't require it either.


This is incorrect. The standard defined # as a "modifier" and says where
the # form differs from the non-# form. Where the standard does not say
that it differs then it must be the same. I.e. for a valid zero valued
argument %#4.2x is required to produce the same output as %4.2x because
nothing in the standard allows it to be different.

Lawrence
Nov 14 '05 #9

P: n/a
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:
:On Wed, 23 Feb 2005 19:59:33 +0000, Walter Roberson wrote:

:> The C89 standard says that # indicates that an alternate output form
:> should be used, and that for hex, non-zero values will have 0x or 0X
:> prepended. You happen to be outputing the zero value, so you have
:> fallen into behaviour that the standard does not nail down. The
:> C89 standard doesn't say that 0x cannot be prepending for the zero value,
:> but it doesn't require it either.

:This is incorrect. The standard defined # as a "modifier" and says where
:the # form differs from the non-# form. Where the standard does not say
:that it differs then it must be the same. I.e. for a valid zero valued
:argument %#4.2x is required to produce the same output as %4.2x because
:nothing in the standard allows it to be different.

The C89 standard defines the result of converting non-zero numbers
with # as the flag (not "modifier" by the way) and x or X as the
conversion, and defines several other alternate conversions. The standard
also says that the result for all other conversions is undefined.
As the standard does not say what happens when converting 0 with %#x
then it falls under the category of "other conversions" and hence
has an undefined result.
--
What is "The Ultimate Meme"? Would it, like Monty Python's
"The World's Funniest Joke", lead to the deaths of everyone who
encountered it? Ideas *have* lead to the destruction of entire cultures.
-- A Child's Garden Of Memes
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.