473,714 Members | 2,531 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 2633
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
3796
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 the highest and lowest of those values:(keep in mind that max, min, bmp, ptr are ulong and w, h are...
6
6652
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 want to see the output::
9
6588
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 using borland bcc32 the command line compiler which seems pretty good, I just dont understand this...
7
96313
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 for posting without verfying things with any standard compiler, i don't have the means for now.
11
1823
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. ============================================================= void ERR(char * err_msg){
4
10295
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. Postgresql developers might note the hackery required to accomodate COPY's insertion of escape...
7
1710
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
1494
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 no and port no correctly but not the IP. And it reads last IP correctly this is the programme
0
8796
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7946
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.