Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:25:24+00:00 2026-06-09T22:25:24+00:00

can anyone explain why the output of main() { printf(hello ); fork(); printf(hello );

  • 0

can anyone explain why the output of

main()   
{   
    printf("hello ");   
    fork();   
    printf("hello ");   
}

is:

hello hello hello hello

and the output of:

main()   
{   
    printf("hello\n");   
    fork();   
    printf("hello ");   
}

is:

hello
hello hello

what difference does \n make w.r.t to buffer?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:25:26+00:00Added an answer on June 9, 2026 at 10:25 pm

    When you fork the memory of the process is copied. This includes stdio buffers, so if the hello stays in the buffer it will be printed by both processes. Both processes go on about their business and eventually flush their buffers and you see “hello” twice.

    Now on most implementations stdout is line-buffered which means a \n triggers a flush. So when the fork happens the buffer is empty. A sure fire way to prevent this would be to flush everything before forking.

    EDIT

    so why does the hello appears twice in the second line of second
    output

    There are now two processes (parent & child) executing the same code so that printf is executed twice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain the difference between the Ruby on Rails console output and the
main() { if(!fork()) while(1) printf(HELLO); else while(1) printf(WORLD); } output :... HELLO HELLO HELLO
Can anyone explain the output of this program and how I can fix it?
Here is a simple php program which gives a strange output. Can anyone explain
Can anyone explain to me why there is a dramatic difference in performance between
Can anyone explain, if Struts Action classes are threadsafe in Struts 1.x. Does RequestProcessor
Can anyone explain why I am getting the output twice here? # include<iostream> #
Can anyone explain to me why this code below does not work? #include opencv/cv.h
Can anyone explain the behaviour of the below code. The output of the below
Here's a puzzler: can anyone explain why cd fails when the output is redirected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.