Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bbn!uwmcsd1!ig!jade!ucbvax!ucsfcgl!fauman From: fauman@cgl.ucsf.edu (Eric Fauman%Stroud) Newsgroups: comp.lang.c Subject: Re: printf and variable length string format (asterisk) Message-ID: <10518@cgl.ucsf.EDU> Date: Mon, 23-Nov-87 20:41:17 EST Article-I.D.: cgl.10518 Posted: Mon Nov 23 20:41:17 1987 Date-Received: Thu, 26-Nov-87 21:32:21 EST References: <692@zycad.UUCP> Organization: UCSF Computer Graphics Lab Lines: 23 In article <692@zycad.UUCP> kjb@zycad.UUCP (Kevin Buchs) writes: > >I want to print out a string whose length I will not know ahead, and which >is not null terminated. I thought the following use of the asterisk >precision delimiter would work: > >char *a = "hello"; int i; >for (i=0; i<6; i++) printf("%*s\n", i, a); > >Does the asterisk not work with the s descriptor? Or, am I using it wrong. The number following the % is a width specifier, which in the case of a string > is the minimum width to use. To specify the precision, the number should follow the %, an optional width field, and a period. The following code will produce the desired output: char *cp; int i; cp = "hello"; for (i=1;i<6;++i) printf("%.*s\n",i+1,cp); Eric Fauman - University of California - San Francisco