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

  • SEARCH
  • Home
  • 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 6649995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:50:26+00:00 2026-05-26T00:50:26+00:00

have a look at this code: #include<stdio.h> #include <unistd.h> int main() { int pipefd[2],n;

  • 0

have a look at this code:

 #include<stdio.h>
 #include <unistd.h>
    int main()
    {
            int pipefd[2],n;
            char buf[100];
            if(pipe(pipefd)<0)
                    printf("Pipe error");
            printf("\nRead fd:%d write fd:%d\n",pipefd[0],pipefd[1]);
            if(write(pipefd[1],"Hello Dude!\n",12)!=12)
                    printf("Write error");
            if((n=read(pipefd[0],buf,sizeof(buf)))<=0)
                    printf("Read error");
            write(1,buf,n);
            return 0;
    }

I expect the printf to print Read fd and write fd before Hello Dude is read from the pipe. But thats not the case… see here. When i tried the same program in our college computer lab my output was

Read fd:3 write fd:4
Hello Dude!

also few of our friends observed that, changing the printf statement to contain more number of \n characters changed the output order… for example..printf("\nRead fd:%d\n write fd:%d\n",pipefd[0],pipefd[1]); meant that Read fd is printed then the message Hello Dude! then the write fd is printed. What is this behaviour??
Note: Out lab uses a linux server on which we run terminals, i don’t remember the compiler version though.

  • 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-05-26T00:50:27+00:00Added an answer on May 26, 2026 at 12:50 am

    It’s because printf to the standard output stream is buffered but write to the standard output file descriptor is not.

    That means the behaviour can change based on what sort of buffering you have. In C, standard output is line buffered if it can be determined to be connected to an interactive device. Otherwise it’s fully buffered (see here for a treatise on why this is so).

    Line buffered means it will flush to the file descriptor when it sees a newline. Fully buffered means it will only flush when the buffer fills (for example, 4K worth of data), or when the stream is closed (or when you fflush).

    When you run it interactively, the flush happens before the write because printf encounters the \n and flushes automatically.

    However, when you run it otherwise (such as by redirecting output to a file or in an online compiler/executor where it would probably do the very same thing to capture data for presentation), the flush happens after the write (because printf is not flushing after every line).

    In fact, you don’t need all that pipe stuff in there to see this in action, as per the following program:

        #include <stdio.h>
        #include <unistd.h>
        int main (void) {
            printf ("Hello\n");
            write (1, "Goodbye\n", 8);
            return 0;
        }
    

    When I execute myprog ; echo === ; myprog >myprog.out ; cat myprog.out, I get:

    Hello
    Goodbye
    ===
    Goodbye
    Hello
    

    and you can see the difference that the different types of buffering makes.

    If you want line buffering regardless of redirection, you can try:

    setvbuf (stdin, NULL, _IOLBF, BUFSIZ);
    

    early on in your program – it’s implementation defined whether an implementation supports this so it may have no effect but I’ve not seen many where it doesn’t work.

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

Sidebar

Related Questions

Please have a look on this code: #include <unistd.h> #include <stdlib.h> #include <stdio.h> int
Have a look at this code: #include <iostream> using namespace std; int main() {
I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
I have written a simple Hello World program. #include <stdio.h> int main() { printf(Hello
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
I have code that I want to look like this: List<Type> Os; ... foreach
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have the following code: #include <iostream> #include <string> void main() { std::string str;
i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int
Take a look at this code: #include <cassert> #ifdef DEBUG #define ASSERT(expr) assert(expr) #else

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.