473,385 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
9 2610
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
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
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
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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CAFxX | last post by:
i'm writing a program that executes some calculations on a bitmap loaded in memory. these calculation ends up with pixel wth values far over 255, but i need them to be between 0 and 255 since i...
6
by: Walter L. Preuninger II | last post by:
I need to convert escape sequences entered into my program to the actual code. For example, \r becomes 0x0d I have looked over the FAQ, and searched the web, with no results. Is there a...
9
by: Ben Midgley | last post by:
Please have a look at the code below My problem is the error message "Nonportable pointer conversion", so far as I can tell there is no pointer conversion occuring here so I am assuming it is...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
11
by: jt | last post by:
Here is my code below, it doesn't printf. But I step it thru using the for statement and use putchar to the display, its there... How can I get it to use printf? If the problem is needing a null...
4
by: djake | last post by:
How can I convert char in wchar_t? And how can I convert wchar_t in char? Thanks to anyone
6
by: Karl O. Pinc | last post by:
These seem to work. I'm posting them now before I'm done with them before I start messing them up with specifics to my database. Any comment is welcome. However, I've no intention of publishing...
7
by: joshua siew | last post by:
heeeeeeeeeeeeeeeeeelpp !!! Can anyone tell me how the heck does VC++ create the ASCII 'ENQ' ???? I'm on a project of programming a PLC devices. I need to send the ENQ ascii value together with...
1
by: dulanjalie | last post by:
im implementating a over lay network. This is the routing table given as a .txt file. node no IP Port no 1 987.000.333.444 5000 2 987.111.333.444 7440 3 222.222.111.111 7000 ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.