473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'#' 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 2616
In article <42************ *********@reade r20.nntp.hccnet .nl>,
Erik Leunissen <lo**@the.foote r.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************ *********@reade r1.nntp.hccnet. nl>,
Erik Leunissen <lo**@the.foote r.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.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <42************ *********@reade r1.nntp.hccnet. nl>,
Erik Leunissen <lo**@the.foote r.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.n rc-cnrc.gc.ca> wrote:

In article <42************ *********@reade r20.nntp.hccnet .nl>,
Erik Leunissen <lo**@the.foote r.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.n rc-cnrc.gc.ca> wrote:
In article <42************ *********@reade r20.nntp.hccnet .nl>,
Erik Leunissen <lo**@the.foote r.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****@netacti ve.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
3789
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 got to write them in a bmp file (in which RGB values are limited to 8 bits per channel). so i need to have them scaled down. first of all i find...
6
6628
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 function that can do this, or do I need to use predefined constants or a table of the values? Below is a sample program. When run with the input w\rx, I...
9
6580
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 something implicit. The error is associated with the assignment { &MenuItemList }, when delcaring the mit_ZoneSelectMenu structure. I am...
7
96263
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 %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me...
11
1805
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 at the end and how can I do this? I tried pos='\0' after my last memcpy and that doesn't work either....
4
10265
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
417
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 production quality code or documentation, these are my notes and I've used published them here in the event that somebody finds them useful. ...
7
1703
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 others data in a string into the plc. I'm also new in VC++ as well as plc... Thanks for the advice in advanced. Rgds,
1
1483
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 This data is stored in t.txt and all data are separated by Tab space. I wrote a program to read the file and store data separately. It reads node...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.