Standard Output

(Last Mod: 27 November 2010 21:38:37 )

ECE-1021 Home



The Standard Output Device on most computers is the console monitor screen - referred to here as the display device. Although some behaviors of the display device are implementation defined, many are defined by the standard and many of the implementation-defined ones follow fairly predictable patterns.

A Useful Model of Many Console Display Devices

Most console display devices appear as a rectangular grid of character positions and are generally numbered from the top left corner starting with Row 1, Column 1. A very common display size for DOS/Windows consoles is 80x25 meaning that there are 80 characters on each line and a total of 25 lines.

At any given time, there is an "active position" on the display. This is simply the location on the screen where the next character will be printed unless something changes the active position first.

In order to foster the highest degree of portability on the greatest number and variety of platforms, the required behaviors when the screen is written to are kept to a minimum. As long as we respect those bounds, we can get consistent output on most platforms that we compile our code on. For instance, when the program starts the active position is initialized in some implementation defined way. It may clear the screen and position the active position in the top-left corner or it may simply place the active position at the start of the line after the one where the command that launched the program was located.

Once the active position reaches the bottom-most line, most display devices will begin scrolling the output. This means that any action that would normally cause the active position to move down will, instead, cause all of the output presently on the display to move up instead.

Defined Behaviors for the Display Device 

If a printing character is written to the display device, a graphical representation of the character is produced at the active position and the active position is advanced one location on the present line. If the starting active position is at the end of the line, then the behavior is implementation specified. The two most common are to wrap the active position to the beginning of the next line or stall the active position in its present location.

All seven of the alphabetic escape sequences define at least some of the expected behavior when written to the display device.

By examining the degree to which each character has defined behavior, a few simple guidelines can be developed which will maximum portability and minimize frustration when developing programs:

  1. If possible, limit output to the printing characters, alerts, new lines, and carriage returns.
  2. Avoid printing to the last position on a line.
  3. Corollary to the above - issue a new line or carriage return prior to the end of the line.
  4. Only use the backspace when the active position is not at the beginning of a line.
  5. Avoid either type of tab as well as form feeds.

Buffered Output

On many implementations the output to the display device will be buffered. This means that characters written to the display are not sent immediately to the host - who's responsibility it is to actually write to the physical display - but will instead hold them into a buffer and transfer them to the host as a block. It is not, therefore, surprising that text written to the screen might not appear right away. The block is normally transferred whenever a new line is written to it. One way to force the contents of the output buffer to send its data to the host is to flush it using the fflush() function accessed via stdio.h. At any point where you want to be sure that the output has made it to the screen - such as after issuing prompts that the user needs to respond to - it is a good idea to flush the buffer, especially if a new line was not the last thing written to the stream.