[PLUG] Weird Output of a C program in Linux

Ashutosh Adkar ashu.adkar at gmail.com
Tue Jul 17 15:28:00 IST 2007

int main ()
 printf ("Hello World!");
 if (fork == 0)
     printf ("I'm the child!\n");
     printf ("I'm the parent!\n");

The output of the above program is :

Hello World!I'm the child!
Hello World!I'm the parent!

Why is "Hello World" being printed twice?
Only the parent should print it right?

If "Hello World" is appended with a "\n" the output is...

Hello World!I'm the child!
I'm the parent!

Hello World is printed only once.Can anybody please explain whats goin on?
My guess is this because the stdout isnt flushed before the child is created.
Please help.
Thanks a lot.

Ashutosh Adkar

More information about the Plug-mail mailing list