Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!cs.utexas.edu!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: print format Message-ID: <3523@goanna.cs.rmit.oz.au> Date: 7 Aug 90 02:34:42 GMT References: <1990Aug2.162651.8083@uunet!unhd> <31530014@hpcvia.CV.HP.COM> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 20 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)