"%#s"?

Ray Butterworth rbutterworth at watmath.waterloo.edu
Tue May 31 02:07:06 AEST 1988


In article <1988May28.222450.2680 at utzoo.uucp>, henry at utzoo.uucp (Henry Spencer) writes:
> > It would be nice for printf et al.  to have a %#s format specifier,
> > to convert unprintable characters into backslash escapes...
> > Has anyone else thought of this?  Is it a good idea?  Can it be put
> > in the standard?
> 
> Yes.  Probably.  No, it's much too late now.
> 
> I came up with this a couple of years ago, although I was more interested
> in the input side, i.e. scanf.  I did not end up making a formal proposal
> to X3J11 because I am a firm believer (firmer than X3J11!) in the "no
> standard without implementation experience" rule, and couldn't point to
> any real experience.  Possibly I should have tried; it WOULD be useful.

We added %#s and %#c to our version of printf a while back,
and it was very useful, especially for error messages. e.g.
1) warning("Illegal character %#x ignored", c);
2) warning("Illegal character '%c' ignored", c);
3) if (isascii(c) && isprint(c))
       warning("Illegal character '%c' ignored", c);
   else
       warning("Illegal character %#x ignored", c);
4) warning("Illegal character '%#c' ignored", c);
The first two are commonly found in many programs; the first
is annoying and the second is wrong.
The third is correct but no one bothers to do it.
The fourth is correct and no trouble to use.

Unfortunately we took out the change for fear that people would use it
and then have their programs break on other systems.

Perhaps %#s and %#c can be added in future updates to the language.

My big complaint with X3J11 regarding this, is that they changed the
definition of \x to take an arbitrary number of hex digits.  That
means that the output of any future %#s could be ambiguous.  Adding
a \z do-nothing escape would solve the problem though.



More information about the Comp.lang.c mailing list