"%#s"?

Karl Heuer karl at haddock.ISC.COM
Wed Jun 1 08:47:30 AEST 1988


In article <1039 at cresswell.quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:
>In article <19166 at watmath.waterloo.edu>, rbutterworth at watmath.waterloo.edu (Ray Butterworth) writes:
>> Perhaps %#s and %#c can be added in future updates to the language.
>
>In the mean-time, why not post sources for a
>	spr_str(char *buffer, int width, int places, char *source)

Since the result is probably going to be handed to printf anyway, I'd get rid
of the width/places arguments and let printf handle them.  This keeps the
function simpler.  (It would, however, probably be a good idea to add a size_t
argument representing the buffer size, to protect against overflow.  Why
repeat sprintf's mistake?)

>spr_str(buf, width, 1, &c) can serve for sprintf(buf, "%#*c", width, c).

Not if c is declared register and/or int (both of which are common when
dealing with text).  I think this case is sufficiently useful and simple that
it deserves to be a separate function: char *spr_chr(int c); the buffer itself
can be static.  (One might also include EOF in the domain.)

This still leaves some questions.  What should be the specifications for the
output of these functions?  In particular, on an ASCII system, should
spr_chr('\1') return "\\1", "\\001", "\\x01", "\\x1\\c", "\\^A", or "^A"?
Should spr_chr('\10') return "\\b"?  What about spr_chr('\\'), does it behave
like other printing chars and return "\\" (that's a string of length 1), or do
we want the output to be completely unambiguous, and return "\\\\" (that's two
backslashes)?

I find that in practice, the answers to the above depend on the application,
which is why I haven't yet installed this function in my private library.
(Also, I'm waiting to see if X3J11 resolves the problem of terminating hex
constants, as it may influence the design decision.)

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



More information about the Comp.lang.c mailing list