Hex escapes in strings

Ray Butterworth rbutterworth at watmath.waterloo.edu
Tue Feb 2 03:12:39 AEST 1988


I too think that allowing arbitrarily long hex strings is not
a good idea, though not for the same reason.

Something that I've always felt should be in the standard printf
functions is allowing the "#" qualifier for %c and %s formats.

"%#c" would print the character as an appropriate escape sequence
if it wasn't a printing character.
(hex vs. octal would be implementation dependent)
e.g. \n, \f, \001, \0xff.

This would make error messages a lot easier to read.
Typically they now say either "Illegal character '%c'\n", or
"Illegal character %#o\n".  The first is very ugly and unreadable
for non-printing characters, the second is unreadable for printing
characters if you haven't memorized the ASCII codes for '*', '}',
etc.  Using "%#c" would give the correct version every time.

Similarly, "%#s" would perform this same expansion on strings.
printf("%#s\n", "one\ntwo");
would produce a single 8 character line of output: "one\ntwo".

If ANSI allows arbitrarily long hex sequences, this scheme
cannot be implemented, since there is no way to indicate
the end of a hex sequence, and so "%#s" output could be
ambiguous.

The previously mentioned suggestion of a null-escape, e.g. "\z",
that did nothing, would solve the problem.



More information about the Comp.lang.c mailing list