Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!pyramid!decwrl!spar!hunt From: hunt@spar.SPAR.SLB.COM (Neil Hunt) Newsgroups: comp.lang.c Subject: Re: printf and variable length string format (asterisk) Message-ID: <122@spar.SPAR.SLB.COM> Date: Mon, 23-Nov-87 14:20:41 EST Article-I.D.: spar.122 Posted: Mon Nov 23 14:20:41 1987 Date-Received: Thu, 26-Nov-87 19:43:46 EST References: <692@zycad.UUCP> Reply-To: hunt@spar.UUCP (Neil Hunt) Organization: SPAR - Schlumberger Palo Alto Research Lines: 39 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); > >I thought it would print: > >h >he >hel >hell >hello > >but I got: > >hello >hello >hello >hello >hello > >Does the asterisk not work with the s descriptor? Or, am I using it wrong. >Is there another way to do this (except fwrite)? The format "%8s" prints a string in a min field width of 8 characters. The format "%.8s" trucactes a string to a max field width of 8. I just tried it, and the same applies to a variable specified field: try: char *a = "hello"; int i; for (i=0; i<6; i++) printf("%.*s\n", i, a); which prints what you were expecting. Neil/.