Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!sdd.hp.com!hp-pcd!hpcvia!brianh From: brianh@hpcvia.CV.HP.COM (brian_helterline) Newsgroups: comp.lang.c Subject: Re: print format Message-ID: <31530014@hpcvia.CV.HP.COM> Date: 6 Aug 90 15:47:07 GMT References: <1990Aug2.162651.8083@uunet!unhd> Organization: Hewlett-Packard Co., Corvallis, Oregon Lines: 31 >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]);