dilemma in control flow

hello im facing a queer problem when i execute the foll code in unix

# include <stdio.h>
# include <unistd.h>

main(int argc,char *argv[])
{
	FILE *fp = fopen("/ras/chirag/fifotest/file.fifo","a");
	int i=1;
	fprintf(fp,argv[1]);
	printf("I SLEEP");
	system("date");
	for (i=0;i<50;i++)
	{
	//printf("do nothing\n");
	}
	i=sleep(25);
	printf("%d",i);
	printf("I WAKE UP");
	system("date");
	fclose(fp);
}


the above code first executes the date command and then sleeps for 25 secs and then prints the strings "I SLEEP" and "I WAKE UP" in the end...

the output i obtained is:

Fri Feb 10 19:10:16 CST 2006
Fri Feb 10 19:10:41 CST 2006
I SLEEP0I WAKE UP

can anyone help me why is such a flow occuring and what can be done to get it the normal way? thanks in advance............

stdout is line buffered

use,

printf("I SLEEP\n");
printf("I WAKE UP\n");

You can flush the buffer with fflush(stdout) or you can do setvbuf(stdout, NULL, _IONBF, 0) at the beginning of the program to turn off the buffer.