printf( "%#04x", 1 )

uunet!summit.novell.com!dfp uunet!summit.novell.com!dfp
Fri Feb 25 10:42:00 PST 1994


> printf( "%#04x", 1 );
>  
> What string should be output by the above:
> 1) 0x01
> 2) 0x0001
> 3) Something else
>  
> In other words, is the '0x' counted in the field width?  If it is, then
> the output is 0x01.  If it is not, then the output is 0x0001.  Current
> vendors do it both ways.

The standard says that
 # -- causes a prefix of "0x" for nonzero values.
 0 -- causes filling with zero instead of space to the field width.
 4 -- specifies the minimum width to be 4 characters (bytes).

There is no indication that zero-fill forces a minimum number of
digits, only that enough 0s are used to reach at least the field
width's worth of characters.  Take away the 0:

	printf("%#4x", 1);

do you expect " 0x1" or "   0x1"?  I would hope that no
implementation produces the latter.  The only trick for the 0 flag
is that the padding goes in a different place than do the spaces.

It's clear to me that the standard specifies "0x01" for the above.

Dave Prosser  dfpasummit.novell.com  SF 1-109 Summit, NJ 07901 (908)522-6227



More information about the Numeric-interest mailing list