(Last Mod: 27 November 2010 21:38:37 )
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.
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.
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.
\a- (alert) (ASCII 0x07 (7 in decimal)
\b- (backspace) (ASCII 0x08) (8 in decimal)
\f- (form feed) (ASCII 0x0C) (12 in decimal)
\n- (new line) (ASCII 0x0A) (10 in decimal)
\r- (carriage return) (ASCII 0x0D) (13 in decimal)
\t- (horizontal tab) (ASCII 0x09) (9 in decimal)
\v- (vertical tab) (ASCII 0x0B) (11 in decimal)
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:
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.