print format
brian_helterline
brianh at hpcvia.CV.HP.COM
Tue Aug 7 01:47:07 AEST 1990
>Is there a way to specify the format in a print statement with a variable.
>What I want to do is print out a group of strings so they are all right
>justified on the right side with the largest string. (example)
>
> format = sizeof(largest_string);
> for (i=0;array[i];i++)
> printf("%[format]s\n",array[i]);
> ^^^^^^^^
> this is what I want to do.
>
>I realize I can write a routine to do this, but I am going to be doing it
>quite a bit and would like to keep the CPU time down to a minimum. Does anyone
>have any ideas?
> Thank you.
>
>
>--
> / | . . . . o o . . . .
>xxxxxxxxxxx|rgd at unhd.unh.edu######> | ^ ^ ^ ^ ^
> \ | --- --- \_/ --- ---
>"Never let an inanimate object throw you" - me. | Never Follow
>----------
How about this:
/* char string[10]; declare this somewhere */
sprintf( string, "%%%ds\n", sizeof(largest_string));
/* string now looks like "%25s\n" or equiv. */
for (i=0;array[i];i++)
printf(string,array[i]);
More information about the Comp.lang.c
mailing list