Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!yale!moran From: moran@yale.UUCP Newsgroups: comp.lang.c Subject: Re: printf and variable length string format (asterisk) Message-ID: <19361@yale-celray.yale.UUCP> Date: Mon, 23-Nov-87 04:54:20 EST Article-I.D.: yale-cel.19361 Posted: Mon Nov 23 04:54:20 1987 Date-Received: Wed, 25-Nov-87 21:55:56 EST References: <692@zycad.UUCP> Sender: root@yale.UUCP Reply-To: moran@yale-zoo-suned.UUCP (William Moran) Organization: Yale University Computer Science Dept, New Haven CT Lines: 47 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); ...some examples omitted > >Does the asterisk not work with the s descriptor? Or, am I using it wrong. >Is there another way to do this (except fwrite)? I think the reason is that the i would specify the minimum field width, and in this case, the field would always be larger than that. Running this will show you what is really going on here. main() { static char a[2] = "h"; int i; for(i=0;i<6;i++) printf("foo%*s string\n",i,a); } it looks like -> fooh string fooh string foo h string foo h string foo h string foo h string So, in answer to your question: no, * sets a min. width not the actual width. Perhaps you should look at the BSD routines which deal with strings which are not null terminated i.e. you could copy the string segement into one which is null terminated and then print that, but it isn't really clear to me whether printing the pieces is important to you. William L. Moran Jr. moran@{yale.arpa, cs.yale.edu, yalecs.bitnet} ...{ihnp4!hsi,decvax}!yale!moran Gonna get my PhD...I'm a teenage lobotomy! - The Ramones