It's unexplainable...

S. Srinivasan srini at ultra.com
Sat Feb 2 07:03:36 AEST 1991


In <1991Jan30.203605.14481 at bronze.ucs.indiana.edu> speelmo at 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 at ultra.com



More information about the Comp.lang.c mailing list