This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: sprintf %hi converion
- To: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- Subject: Re: sprintf %hi converion
- From: Kaz Kylheku <kaz at ashi dot footprints dot net>
- Date: Fri, 29 Jun 2001 15:59:23 -0700 (PDT)
- cc: Matt Wimer <matt at god dot vtic dot net>, <libc-alpha at sourceware dot cygnus dot com>
On Fri, 29 Jun 2001, Joseph S. Myers wrote:
> Date: Fri, 29 Jun 2001 22:32:20 +0100 (BST)
> From: Joseph S. Myers <jsm28@cam.ac.uk>
> To: Matt Wimer <matt@god.vtic.net>
> Cc: libc-alpha@sourceware.cygnus.com
> Subject: Re: sprintf %hi converion
>
> On Fri, 29 Jun 2001, Matt Wimer wrote:
>
> > Conversion of an int using %hi doesn't cast the va_arg (ap, int) to
> > 'short int' like freebsd, solaris, and netbsd. I'm guessing
If the expression you are trying to print with %hi exceeds the
range of the type short, then you are calling for a nonportable
conversion. So the behavior doesn't affect programs that avoid
implementation-defined behavior.
> > that this is a bug because it doesn't conform to the man pages
> > which seem to be pretty definative. This may not be in the
> > posix spec but it seems everyone else deals with this bad input
> > the same way.
>
> See the appended comp.std.c messages. The experts seem to differ in this
> area; you could always bring it up again on comp.std.c, or elsewhere.
You can just use the int conversion specifiers for values of type
short; the type int can represent all of the values of short, and short
is in fact promoted to int. So it's correct to just use plain %d or %i
when the corresponding expression has type short (promoted to int).
unsigned short can promote to int or unsigned int, which is
disambiguated with a simple cast: printf("%u", (unsigned) ushort_var);
Thus, the h modifier is an entirely superfluous invention.