[PLUG] Weird Output of a C program in Linux
Pramod Sonar
pramod5393 at gmail.com
Tue Jul 17 15:39:59 IST 2007
Hi Ashu
you are right ! Hello world should be print only ones!
but it's printing twice as when printf executes it does not send data to
output buffer.
so if use \n their or fflush(stdout); after printf ,or see the comment that
I have put in your code, it will print Hello World single times.
Regards,
Pramod
"The wind & the waves are always on the side of the ablest navigators" ----
Edward Gibbon
On 7/17/07, Ashutosh Adkar <ashu.adkar at gmail.com> wrote:
>
> int main ()
> {
> printf ("Hello World!"); // printf("hello World\n");
> if (fork == 0)
> printf ("I'm the child!\n");
> else
> 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.
>
>
>
>
> --
> Regards,
> Ashutosh Adkar
>
> --
> ______________________________________________________________________
> Pune GNU/Linux Users Group Mailing List: (plug-mail at plug.org.in)
> List Information: http://plug.org.in/cgi-bin/mailman/listinfo/plug-mail
> Send 'help' to plug-mail-request at plug.org.in for mailing instructions.
>
--
More information about the Plug-mail
mailing list