"%#s"?

der Mouse mouse at mcgill-vision.UUCP
Sun Jun 12 21:15:27 AEST 1988


In article <4311 at haddock.ISC.COM>, karl at haddock.ISC.COM (Karl Heuer) writes:
> 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.
>> 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.

If you are going to use a static buffer, folks, please use several of
them, or otherwise arrange that it doesn't lose big if I say

printf("  in_chr = %s, out_chr = %s\n",
	spr_chr(in_chr), spr_chr(out_chr));

With a domain as small as a char, it would even work to

static char *strings[] = { "EOF", "^@", "^A", ...., "\\377" };
/* or whatever strings you want */
/* this example assumes EOF is -1 */

char *spr_chr(int chr)
{
 return(((chr==EOF)||(chr==(int)(unsigned char)chr))?strings[chr+1]:"OOPS");
}

(by the way, is the chr==(int)(unsigned char)chr test a safe way of
testing whether the value is one an unsigned char could take on?)

					der Mouse

			uucp: mouse at mcgill-vision.uucp
			arpa: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list