15

Why should we use stderr when printing a custom error message from printf works fine?

For example why use stderr at all when I can just write a statement like this:

    printf("error! you didn't... blah blah blah");
2
  • 3
    Because it's called standard error for a reason (to be used to immediately display error messages)?
    – Ken White
    Commented Nov 8, 2013 at 23:30
  • Not to mention you would not get that error in your example until something else prints a '\n' or after the program terminates, because stdout is buffered while stderr is not. Commented Nov 9, 2013 at 0:45

5 Answers 5

23

It is good practice to redirect all error messages to stderr, while directing regular output to stdout. It is beneficial to do this because anything written to stderr is not buffered, i.e., it is immediately written to the screen so that the user can be warned immediately.

0
22

stderr stands for standard error stream.

In console programming, it is the console -- the screen. It is essentially the same as stdout.

The general practice is to redirect all error messages to stderr and all regular output to stdout.

The reason is that it is possible to redirect standard output to a file instead of the screen. So If you perform a dir > dirlist.txt command on the command prompt, the directory listing goes into the text file instead of the screen. If you code to redirect error messages to stderr, the errors will always go to the screen instead of the file so that the user could be warned immediately instead of ending up seeing unexpected results in the file.

Using printf() will display the error message to the console, which will be stored in the stdout buffer, but using stderr, is different.

stderr can be used as an argument for any function that takes an argument of type FILE* expecting an output stream, like fputs or fprintf.

Although in many cases, both stdout and stderr are associated with the same output device (like the console), applications may differentiate between what is sent to stdout vs stderr for situations when one of them is redirected. For example, it is a common practice to redirect the regular output of a console program (stdout) to a file, while expecting the error messages to keep appearing in the console.

It is also possible to redirect stderr to another destination from within a program using the freopen function.

Also, stderr is never fully buffered on startup. It is library-dependent whether the stream is line-buffered or not-buffered by default (see setvbuf).

3
  • stdout can also be used as an argument of type FILE*. Great answer though.
    – mgarey
    Commented Dec 27, 2016 at 17:28
  • 3
    When using shell pipelines writing errors to stdout would cause pipelines like this to do wonky things: some_command | cut -f1 -d: | sort -u Writing errors to stderr ensures that they still appear on the screen but don't mess up the pipeline. Commented Jan 19, 2022 at 20:39
  • good point @MarkE.Hamilton Commented Jan 20, 2022 at 14:42
9

It is a good practice.

Lets say you use linux. If so, you can run your program following way:

./program >out 2>errout

'out' file will contain only STDOUT.
'errout' file will contain only STDERR

So if your output is hundreds of lines long, it is easier to look for few errors in 'errout' rather than look through tons of non-error lines combined with error lines.

1
  • This is the answer I was looking for. Question is, do applications normally do this; such as java, javac, g++, gcc, ar, etc? Commented Aug 17, 2021 at 23:14
1

In addition to stderr not being buffered, it is also nice to split output into errors vs. normal output to allow other programs to use of your program more easily. That way, the calling program can redirect standard or error output selectively depending on what it needs to know. This same facility can be used manually through Unix shells - here's one way I use that sometimes:

% find / -iname hello.txt
find: /.DocumentRevisions-V100: Permission denied
find: /.fseventsd: Permission denied
find: /.MobileBackups: Permission denied
find: /.Spotlight-V100: Permission denied
find: /.Trashes: Permission denied
^C
% find / -iname hello.txt 2>/dev/null <-- filter "Permission denied" errors
1
  • If I just used >, or if I used 1>, it would redirect stdout to the specified file. In this case, file descriptor 2 (stderr) is being redirected to the file /dev/null.
    – Dan
    Commented Nov 8, 2013 at 23:37
0

If you redirect your program's output into a file you still would want to see the errors on screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.