Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!rpi!zaphod.mps.ohio-state.edu!mips!ultra!srini From: srini@ultra.com (S. Srinivasan) Newsgroups: comp.lang.c Subject: Re: It's unexplainable... Message-ID: <1991Feb1.200336.491@ultra.com> Date: 1 Feb 91 20:03:36 GMT References: <1991Jan30.203605.14481@bronze.ucs.indiana.edu> Organization: Ultra Network Technologies Lines: 29 In <1991Jan30.203605.14481@bronze.ucs.indiana.edu> speelmo@bronze.ucs.indiana.edu (Lance Speelmon - UCS) writes: >Would anyone please explain to me why my output is not what I am >expecting... Here is a script of my source and the output... >void main(void){ > printf("The date is: "); > system("date"); > printf("\n"); >} >Wed Jan 30 15:16:17 EST 1991 >The date is: "printf" uses stdio which uses buffering in that it does not actually send output to the stdout until : 1. It hits the buffer limit, OR 2. It hits a newline (not too sure of this one), OR 3. the calling process exits. "system" call forks a subshell to print out the date, which again may use stdio buffering, but the child process exits and the buffers are thus flushed. The parent "main" process which was blocked waiting for the child then continues until it exits. Only at this point are its buffers flushed. Thus... To fix it, you might try looking at setbuf(2). S. Srinivasan srini@ultra.com