print format

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Tue Aug 7 12:34:42 AEST 1990


In article <31530014 at hpcvia.CV.HP.COM>, brianh at hpcvia.CV.HP.COM (brian_helterline) writes:
> >Is there a way to specify the format in a print statement with a variable.
There isn't any way to specify the *format*, but each of the numeric operands
of an edit descriptor can be dynamically specified.  You put an asterisk in
place of the number, and the actual number goes in the argument list.

> >	format = sizeof(largest_string);
> >	for (i = 0; array[i]; i++)
> >		printf("%[format]s\n",array[i]);
> >			     ^^^^^^^^
Change the last line to
		printf("%*s\n", format, array[i]);

I often find myself doing
	printf("%.*s", maxlen, s)
to truncate strings that might be rather long.
-- 
Distinguishing between a work written in Hebrew and one written in Aramaic
when we have only a Latin version made from a Greek translation is not easy.
(D.J.Harrington, discussing pseudo-Philo)



More information about the Comp.lang.c mailing list