Printf format strings 
Author Message
 Printf format strings

   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).
--
          Jeremy Grodberg

Usenet: ...rochester!kodak!grodberg



Thu, 17 Sep 1992 18:27:00 GMT  
 Printf format strings

Quote:
> In our Sun C compiler, printf("%#07x", 27); prints "0x000001b" (9 characters)
> while printf("%#7x", 27); prints "   0x1b" (7 characters). Is this ...
> discussed in the ANSI standard?

Well, to my eye this section of the *draft proposed* standard is unclear.
The directly relevant bits of 4.9.6.1 are:

#   If the converted value has fewer characters than the field width,
#   it will be padded ... to the field width.  The padding is with spaces
#   unless the field width integer starts with a zero, in which case the
#   padding is with zeros.
        ...
#   The flag characters and their meanings are:
        ...
#   The result is to be converted to an "alternate form".  ...  For x
#   ... conversion, a nonzero result will have 0x ... prepended to it.

The use of "converted value" together with "converted to an alternate form"
seems to require the first printf to produce "0000x1b", and the second one
to produce what it actually does.  My formal comments to ANSI included a
rewrite of 4.9.6.1 to eliminate a number of other problems with the section,
including some problems with zero-padding, but I must admit I missed this one.

I would guess that the intention was to require "0x0001b" and "   0x1b".
Any semi-official comments?

Mark Brader                     "But I do't have a '' key o my termial."
utzoo!sq!msb                                               -- Lynn Gold



Thu, 17 Sep 1992 17:34:00 GMT  
 Printf format strings
 >
 >   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



Thu, 17 Sep 1992 16:27:00 GMT  
 Printf format strings

Quote:
>  No printf() function I've used in the last 5 years supports the
>"#" designator.

In other words, you haven't used:

        VAX 4.2BSD
        VAX 4.3BSD
        Any System III or System V release from AT&T
        SunOS
        probably Ultrix-32
        lots of other systems

in the last 5 years.

It's even *documented* in the S3 and S5 manuals.

Quote:
>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,

Which he will.


Thu, 17 Sep 1992 16:09:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. printf format string: how bad style is this?

2. printf() format-string parser wanted!

3. printf Format string

4. Format string as a parameter to printf()?

5. printf -*s format string

6. Examples of format strings for scanf/printf ?

7. printf and formatting numeric strings

8. placing output of printf in memory: formatting strings

9. printf and variable length string format (asterisk)

10. Source to format strings like printf()

11. STL strings & printf-type formatting

12. replacing printf with my printf, then calling C lib's printf

 

 
Powered by phpBB® Forum Software