[comp.std.c] printf format

karl@haddock.ima.isc.com (Karl Heuer) (12/14/89)

In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>[Can printf() format numbers with embedded commas?]

Not in K&R or ANSI C, though it's conceivable that some vendor has added it as
an extension.

You may be thinking of the localization features of ANSI C.  These provide
some support for determining what the local convention is (e.g. where to place
the commas, what the currency symbol looks like, etc.), but there's no library
function that actually makes use of any this information (except for the
decimal-point character).

>If printf does not have this ability, it should be added.

I think a better idea is to have a separate function to do this type of
formatting, then print it with %s.  (Just like you have to do with a time_t,
for example, to make it print in a human-readable format.)  I suspect this is
what the Committee had in mind.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

jmm@eci386.uucp (John Macdonald) (12/16/89)

In article <15430@haddock.ima.isc.com> karl@haddock.ima.isc.com (Karl Heuer) writes:
>In article <5761@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>>[Can printf() format numbers with embedded commas?]
>
>Not in K&R or ANSI C, though it's conceivable that some vendor has added it as
>an extension.

 [...]

>>If printf does not have this ability, it should be added.

>I think a better idea is to have a separate function to do this type of
>formatting, then print it with %s.  (Just like you have to do with a time_t,
>for example, to make it print in a human-readable format.)  I suspect this is
>what the Committee had in mind.

It's too late for this round of the ANSI spec (and perhaps too far
from existing practice anyhow), but the method I have used anytime
I've had to implement my own printf family of routins for a system
is to have the low-level format scanner collect a % sequence
(%<opt_stuff><char>) and then call a function to carry out the action
that corresponds to <char>.  The function is found in a table indexed
by <char>.  It is passed arguments indicating provided (or default)
values for min_width, max_width, lead_zero, left_justify, and arg_pointer,
and returns the updated arg_pointer as return value (I was doing this
before varargs came onto the scene)).  By providing a get and a set
routine to inspect and modify entries in the table, a number of useful
capabilities became available - most usefully, program-defined extensions
to the printf format codes - e.g. %T to print a symbol table entry.
-- 
80386 - hardware demonstrating the fractal nature of warts.   | John Macdonald
EMS/LIM - software demonstrating the fractal nature of warts. |   jmm@eci386