Thread: stderr vs stdout

  1. #1
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326

    stderr vs stdout

    What is the different between the two?

    fprintf(stdout, "Hello there\n");

    Produces the same output as

    fprintf(stderr, "Hello there\n");

    Is there a practical use for stderr? Is it logged or something? Or I am thinking maybe it is suppose to be different but depends on the OS, does Linux use stderr the same way as DOS? Or does it log it or something?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Within the confines of the C standard, stdout is fully buffered and stderr is not buffered. In other words, stdout will flush the buffer when the programmer explicitly asks for it or when it is most convenient, stderr writes the message immediately. Basically, stdout should be used for regular messages and stderr for diagnostic (error) messages.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Why should stderr be used for error messages? I still do not understand.

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by JoshG
    Why should stderr be used for error messages? I still do not understand.
    many users (smarter ones, not windoze idiots) will redirect stdout and stderr to different places at some times, and it doesn't help them if your error messages are not logically organized as such.
    hello, internet!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why should stderr be used for error messages?
    Diagnostic messages are commonly used for debugging, when debugging you want to change the effect of the program as little as possible from the production version. By using unbuffered output, you don't effect the flow of data nearly as much as you would by using buffered output, and thus gain a more effective debug version of the program. Of course, the obvious benefits of the error message being printed right away are understandable.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    Thanks, I was just wondering.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can pipe stderr output to a file. Also you can redirect both stdout and stderr.

    Code:
    FILE *myerr = freopen("error.log", "wb", stderr);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  2. Redirecting stdout, stderr and stdin
    By iwabee in forum Linux Programming
    Replies: 9
    Last Post: 05-16-2005, 05:42 PM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. receiving stderr and stdout from popen
    By rotis23 in forum C Programming
    Replies: 1
    Last Post: 11-08-2002, 08:21 PM