Print Window
Volume Number: 2
Issue Number: 10
Column Tag: ABC's of C
A Print Window for Debugging 
By Bob Gordon, Apropos Publications, Contributing Editor
In the first column we did the traditional first C program and wrote "hello,
world" on the screen using the C function printf(). Since then we have examined some C
programming concepts and gotten a window and menus to appear on the screen.
Beginning with this column we are going to spend some time writing to a window. I am
going to start by writing some text. My main reason for wanting to start with text is
the complaint I had with the LightSpeed C compiler: there was no way to write to the
screen and still see the menus and window you put there. Since one of the most useful
debugging tools is the ability to print out the value of variables and to get some
indication of where you are in a program, I thought a useful and instructive function to
write would be a printf()-like routine that would not take over the entire screen. I
called the function printw() (print to window), and it turned out to be fairly complex
and include some rather obscure C. It is not that important if you don't follow the
entire explanation. There is a lot of standard C presented in this month's column, and
the function will prove to be quite useful. Type it in and get it working. By the way, it
is not finished, and we will add to it in subsequent installments.
C Text Conventions
C compilers provide an escape mechanism in text to allow the placement of
non-displayable characters inside a string. The escape character is the backslash (\).
Certain characters preceded by the backslash result in a control character. Note that
the control character is placed in the string replacing the backslash and the following
character. These sequences are referred to in The C Programming Language as
character constants.
Sequence ASCII Description
\n LF line feed
\t HT horizontal tab
\v VT vertical tab
\b BS backspace
\r CR carriage return
\f FF form feed
\" double quote
\' single quote
\\ backslash
To use these, just place the sequence where you wish to have the character:
char c;
char *s;
c = '\b';
s = "\nhello, world\n";
You may also follow the backslash with one to three octal (octal!) digits to
generate any arbitrary character. You will most often see '\0' which is the null
character.
Fig. 1 Our printw() window is out of the way
Loops
Except for the event loop, we have not had much reason to use loops so far. This
month, however, there are two functions that scan through strings, and I have used two
C looping constructs.
To have a loop that tests before the code is run, use the while loop.
while (expression )
statement
The expression may be any C expression. If the expression is true (non-zero),
the statement is executed. If the expression is false (zero), the loop is terminated and
the expression is not executed.
A second type of loop tests at the bottom. This means that the body of the loop is
executed at least once.
do
statement
while (expression );