4 Replies - 1459 Views - Last Post: 14 January 2012 - 05:54 PM Rate Topic: -----

#1 aacuna3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 12

Difference between stdout and stderr when used in fprintf()

Posted 14 January 2012 - 05:20 PM

What is the difference between using stdout and stderr when using fprintf() to output? I am having a little bit of trouble understanding the difference between the two. Take the following code as an example:

 
  #include <stdio.h>
  #include <unistd.h>
  int main()
  {
          while(1)
          {
		fprintf(stdout,"hello-out");
		fprintf(stderr, "hello-err");
		  sleep(1);
          }
          return 0;
  }




When I run this program, fprintf(stdout,"hello-out") does not produce an output immediately. Instead the output produced by fprintf(stderr, "hello-err") prints out for a while before fprintf(stdout,"hello-out") produces an output. Why exactly is that? I read that this was due to the fact that the output produced by stdout is buffered, while stderr isn't. If this is the case can someone please elaborate.

Let me know if the question is not clear enough. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between stdout and stderr when used in fprintf()

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Difference between stdout and stderr when used in fprintf()

Posted 14 January 2012 - 05:30 PM

Basically stderr and stdout are two different output streams, by default consoles display them both, but they don't have to. On bash consoles for example I can output the stderr output to a seperate file like so: ./program 2>error_file. Or when I'm interested in the ouput and don't care about the errors ./program 2>/dev/null. Piping/ignoring the regular output is also possible by using 1 instead of 2.

After looking it up, it appears indeed that stderr is not buffered, so that would explain why it would appear earlier. It appears before the stdout buffer is flushed.

As for the programmer, use them for what they were designed for. Pipe output to stdout and runtime errors/warnings to stderr and let the user decide how he wants to handle it.

This post has been edited by Karel-Lodewijk: 14 January 2012 - 05:35 PM

Was This Post Helpful? 1
  • +
  • -

#3 aacuna3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 12

Re: Difference between stdout and stderr when used in fprintf()

Posted 14 January 2012 - 05:46 PM

That was very helpful. Thanks.

I have another question that is related. When I add a new line character('\n') to the stdout output,
fprintf(stdout, "hello-out \n"); 


it creates an output immediately and does not take as long as it did without the newline character. I am just wondering why this is the case.
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Difference between stdout and stderr when used in fprintf()

Posted 14 January 2012 - 05:51 PM

stdout is line buffered in C, by outputting \n you implicitly flush the buffer.
Was This Post Helpful? 0
  • +
  • -

#5 aacuna3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-January 12

Re: Difference between stdout and stderr when used in fprintf()

Posted 14 January 2012 - 05:54 PM

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1