🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2285

Re: 'h' modifier in printf

#2285
From: baynes
Date: Thu, 14 Mar 1996 13:55
77 lines
3573 bytes
Michael J Zehr (tada@athena.mit.edu) wrote:

: I was recently asked a question about printf whose answer I couldn't
: determine by reading K&R2 (and alas the company doesn't have a copy of
: the standard to refer to).

>From the copy of the standard in Plauger's book:
	"An optional h specifying that a following d, i, o, u, x or X conversion
	specifier applies to a short int or unsigned short argument (the argument
	will have been promoted according to the integral promotions, and its value
	shall be converted to short in or unsigned short int before printing);..."

: The "h" modifier says the corresponding argument will be printed as a
: short or unsigned short.

: So, given:

: short s;
: printf("%d", s);
: printf("%hd", s);

: (Assuming of course that s has been initialized at some point.)

: Can these two ever be different?  I'm aware of course that the short is
: widened to an int during the function call, but this preserves the
: value.

The should never be different.

: (A totally non-relevant piece of information is that on at least one
: platform there is never a difference between these two for any value of
: s from SHRT_MIN to SHRT_MAX.  But that doesn't answer the question.)

Good.

: This is the main question I'm interested, but as a followup, if these
: always result in the same output, why is the 'h' modifier defined in the
: first place?

Probably so you can use the same format string with scanf.

: Some speculations:

: int i;
: printf("%hd", i);
: printf("%d", (short)i);

: This question is stretching a bit to try to find what if anything the
: 'h' modifier is ever used for.  Certainly the first line would have a
: different result without the 'h', but casting seems like it ought to
: have the same result.

I think if the value of i is outside SHRT_MIN to SHRT_MAX then both are
undefined for different reasons. The first printf (%hd) because the conversion
does not apply to a short integer. The second printf (%d of (short)) because
the cast of i to (short) is not defined when i is out of range. If you had
used unsigned numbers and the u conversion then the second would become
defined because casting to a shorter unsigned number is defined, the first 
would still be undefined. If i is in the range SHRT_MIN to SHRT_MAX you are
probably OK. However one could argue that a perverse implementation might 
have some hidden means of detecting that the integer passed to printf was
actually taken from a short or an integer and then objecting in this case if
it did not come from as short as the standard specifies.

I understand different real implementations will behave differently if
the value passed to an 'h' modified conversion is out of range for the
appropriate short type.

'h' adds nothing useful over not using it, it just increases the potential
for undefined behaviour.


--
Stephen Baynes                              baynes@ukpsshp1.serigate.philips.nl
Philips Semiconductors Ltd
Southampton                                 My views are my own.
United Kingdom
Technical Details
Message-ID: <4i98fl$8ml@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4i801c$455@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: baynes@ukpsshp1.serigate.philips.nl
Newsgroups: comp.lang.c.moderated,comp.std.c
Subject: Re: 'h' modifier in printf
Followup-To: comp.lang.c.moderated,comp.std.c
Date: 14 Mar 1996 07:55:01 -0600
Organization: Digital Solutions
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4i98fl$8ml@solutions.solon.com>
References: <4i801c$455@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com