Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!rpi!bu.edu!m2c!wpi.WPI.EDU!profesor From: profesor@wpi.WPI.EDU (Matthew E Cross) Newsgroups: comp.lang.c Subject: Re: It's unexplainable... Message-ID: <1991Jan31.121342.16825@wpi.WPI.EDU> Date: 31 Jan 91 12:13:42 GMT References: <1991Jan30.203605.14481@bronze.ucs.indiana.edu> Organization: Worcester Polytechnic Institute Lines: 41 In article peterj@swanee.ee.uwa.oz.au (Peter Jones) writes: >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... >>% cat foo.c >>#include > >>void main(void){ >> printf("The date is: "); >> system("date"); >> printf("\n"); >>} >>% a.out >>Wed Jan 30 15:16:17 EST 1991 >>The date is: > >There are two processes running here, one is the "foo" program and the >other is the "date" program. It will depend on the scheduling what actually >happens - I guess. It would need "foo" to either flush its output buffer >or to do an input or to write to "stderr" (& not be buffered). No, no, no. Well, OK, there are two processes, but nothing depends on the scheduling. "foo" will run, start the subprocess "date", which runs and finishes, then "foo" will continue. What causes this is the line buffering of the stdio functions. Whenever you print anything, it goes into a buffer, until you send a '\n' character, when the routines receive a '\n' character, they flush the output buffer. (this is also why 'getchar()' won't to anything until you hit a return, it waits to flush the buffer). Also, the "date" program prints a newline before it prints the date. To avoid the line buffering, you would have to use 'setbuf()' or something like it. -- +-------------------------------------+---------------------------------------+ | "The letter U has a lot of uses ... | profesor@wpi.wpi.edu | | I like to play it like a guitar!" +---------------------------------------+ | -Sesame Street | Make love, not War.. |