Using Standard Error

The standard error stream is, by default, attached to the terminal screen along with the standard output. However, programs should not regard them as equivalent. Normal results from a program should be sent to the standard output stream. Errors, warnings, and other information that is meant to inform the user about the condition of the program rather than present results from the computations, should be sent to the standard error.

Having two separate output streams for results and messages allows the user of the program to separate error and warning messages from normal output using redirection:

shell> a.out < input.txt > output.txt 2> errors.txt
        

This will be important to users who want to store screen output in a file for further processing. In this case, they will not want the output contaminated with error and warning messages.

Users may also want to see errors and warnings on the screen while redirecting results to a file. The following command reads keyboard input from the file input.txt and send results meant for the screen to output.txt. Anything the program prints to the standard error stream (ERROR_UNIT), however, will be written to the screen.

shell> a.out < input.txt > output.txt
        
Practice

Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
  1. Why is it important to separate error messages from normal output in all of our programs?