Thread: When to print to stderr?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89

    Cool When to print to stderr?

    Of course, when there is an error, that much I understand. But what about these cases?
    • User doesn't know how to run the program, so he (or she) simply runs it without parameters to get information. The program replies:
      Usage: TheProgram [Options] Infile Outfile.
      Stderr or stdout? My guess is stderr.
    • User wants help and runs
      Code:
      TheProgram --help
      Output to stderr or stdout? I would guess stderr, but stdout makes sense too, I think.
    • User wants to know the program's version and runs something like
      Code:
      TheProgram --version
      Is the output supposed to go to stdout or stderr? I would guess stdout for this one.


    Just trying to do things right…

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    1): You should prefer `stderr' because the client hasn't correctly used the tool.

    2): You should prefer `stdout' because the output from "--help" isn't an error message.

    3): You should prefer `stdout' because the output from "--version" isn't an error message.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Think about running this with various options:
    Code:
    The Program > out.txt
    What do you want to show up in out.txt?

    I really think the answer is kinda obvious. A switch like --version or --help usually means that the user is requesting the output on purpose.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,114
    Without any redirection, the three standard predefined files input and output as follows:

    stdin: Normally input from the keyboard

    stdout: Normal output of your program to the screen

    stderr: Error messages you write will go to the screen, even when stdout is redirected, unless stderr is also redirected.

    Providing you are running on Linux/Unix, see `man stdin` for details.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    That all make sense to me now. Thanks!

    And yes, 100 % Linux here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stderr
    By Vespasian in forum C Programming
    Replies: 8
    Last Post: 09-14-2011, 10:24 AM
  2. stderr question
    By -EquinoX- in forum C Programming
    Replies: 10
    Last Post: 03-05-2008, 10:32 PM
  3. how do you write to stderr
    By -EquinoX- in forum C Programming
    Replies: 7
    Last Post: 02-12-2008, 05:58 PM
  4. stderr vs stdout
    By JoshG in forum C Programming
    Replies: 6
    Last Post: 10-20-2002, 11:17 PM

Tags for this Thread