[comp.lang.c] print format

unhd (Roger G Desroches II) (08/02/90)

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@unhd.unh.edu######>              |  ^    ^    ^    ^    ^
	    \                                    | ---  ---  \_/  ---  ---
"Never let an inanimate object throw you" - me.  |       Never Follow    

browns@astro.pc.ab.com (Stan Brown (216)371-0043) (08/03/90)

In article <1990Aug2.162651.8083@uunet!unhd>, rgd@uunet!unhd (Roger G Desroches II) writes:
> 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.
> 

Do it this way:
	format = strlen(largest_string);
	for (i=0; arraay[i]; ++i)
	    printf("%*s\n", format, array[i]);

Stan Brown, Oak Road Systems
#include <disclaimer.h>
Sometimes even I don't stand behind my opinions; don't expect anyone else to!

ark@alice.UUCP (Andrew Koenig) (08/03/90)

In article <1990Aug2.162651.8083@uunet!unhd>, rgd@uunet!unhd (Roger G Desroches II) writes:

> 		printf("%[format]s\n",array[i]);
> 			     ^^^^^^^^
> 				 this is what I want to do.

Do it this way:

	printf("%*s\n", format, array[i]);
-- 
				--Andrew Koenig
				  ark@europa.att.com

brianh@hpcvia.CV.HP.COM (brian_helterline) (08/06/90)

>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@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]);

ping@cubmol.bio.columbia.edu (Shiping Zhang) (08/07/90)

In article <31530014@hpcvia.CV.HP.COM> brianh@hpcvia.CV.HP.COM (brian_helterline) writes:
>>
>>	format = sizeof(largest_string);
>>	for (i=0;array[i];i++)
>>		printf("%[format]s\n",array[i]);
>>			     ^^^^^^^^
>>				 this is what I want to do.
>How about this:
>
>	/* char string[10];  declare this somewhere */
>
>	sprintf( string, "%%%ds\n", sizeof(largest_string));
                         ^       ^ double quots would be missing from
                                   string, should be modified a little bit
>	/* string now looks like "%25s\n" or equiv. */
>	for (i=0;array[i];i++)
>		printf(string,array[i]);

The following should also work:

	format = sizeof(largest_string);
	for (i=0;array[i];i++)
		printf("%*s\n",format,array[i]);


-ping

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (08/07/90)

In article <31530014@hpcvia.CV.HP.COM>, brianh@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)