>
> In our Sun C compiler, printf("%#07x", 27); prints "0x000001b" (9 characters)
>while printf("%#7x", 27); prints " 0x1b" (7 characters). Is this a) common,
>b) "correct", c) discussed in the ANSI standard? My feeling is that the two
>format strings should always generate 7 characters (unless more are necessary
>to represent the number), and losing this battle I would at least hope they
>would both generate 9 characters (either the "0x" is part of the field width
>or it isn't).
No printf() function I've used in the last 5 years supports the
"#" designator. Since, by your example, I'd guess it's "suppost" to
close-prefix the number by the "0x" designator, my primary suggestion
would be that unless you find this to be in the ANSI standard, that you
stop using it in any code you expect to be able to port anyplace. You
can take a look at K&R page 145 to see that this is Not included in the
"K&R standard" and thus, is probably not avaiable on a large number of
machines.
I have no idea if "#" is supported by the ANSI standard. If it is, then
the standard is the place to look (I have no idea which way they'd define
it...) If not, you'll need to use a different method of getting a right
justified hex constant with-close-preceding "0x" if you expect to be able
to port it to anyplace...
If I had to, I'd probably use:
static char cphbuf[20] = "0x";
.....
sprintf(&cphbuf[2], "%x", 27);
printf("%7s", cphbuf);
.....
which should work the same way on any system....
---
Software Consultant - DynaSoft Systems
UUCP: ...{amdahl,ihnp4,rutgers}!{meccts,dayton}!viper!john