[comp.std.c] ANSI printf

mitch@manta.NOSC.MIL (Ray Mitchell) (03/08/91)

This question concerns the use of the "h" modifier in a printf conversion
specification.  According to ANSI C, %hi, %hd, etc. indicate that the
corresponding argument is to be printed as a short integer.  However, since
all but the first printf argument are prototyped as being variable arguments,
the usual default argument type conversions must occur.  Therefore, any
argument of type short will be converted to int before being passed.  If ints
and shorts are the same size, no problem.  If ints are larger than shorts,
i.e., 32 bit ints and 16 bit shorts, still no problem since printf can
use either all 32 bits or only the original 16 and the value will be the
same.  As far as print format is concerned, doesn't a short get printed in the
same format as an int?  If this is the case, I fail to see any reason for the
existance of "h" since some bizzare form of "lint" would be the only thing
that could detect a type mismatch.  (I know there must be something I
am missing on this.)


				Ray Mitchell

meissner@osf.org (Michael Meissner) (03/08/91)

In article <1830@manta.NOSC.MIL> mitch@manta.NOSC.MIL (Ray Mitchell) writes:

| 
| This question concerns the use of the "h" modifier in a printf conversion
| specification.  According to ANSI C, %hi, %hd, etc. indicate that the
| corresponding argument is to be printed as a short integer.  However, since
| all but the first printf argument are prototyped as being variable arguments,
| the usual default argument type conversions must occur.  Therefore, any
| argument of type short will be converted to int before being passed.  If ints
| and shorts are the same size, no problem.  If ints are larger than shorts,
| i.e., 32 bit ints and 16 bit shorts, still no problem since printf can
| use either all 32 bits or only the original 16 and the value will be the
| same.  As far as print format is concerned, doesn't a short get printed in the
| same format as an int?  If this is the case, I fail to see any reason for the
| existance of "h" since some bizzare form of "lint" would be the only thing
| that could detect a type mismatch.  (I know there must be something I
| am missing on this.)

Consider on a machine that has 16 bit shorts and 32 bit longs:

	short i = 0x8000;

	printf ("%#hx", i);

you want 0x8000 printed, and not 0xffff8000.
--
Michael Meissner	email: meissner@osf.org		phone: 617-621-8861
Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142

Considering the flames and intolerance, shouldn't USENET be spelled ABUSENET?