Page 121 - Programming Microcontrollers in C
P. 121
106 Chapter 2 Advanced C Topics
{
return(bufp>0) ? buf[—bufp] : getchar();
}
void ungetch(int c) /* put character back onto
input */
{
if(bufp>=BUFSIZE)
printf(“ungetch: too many characters \n”);
else
buf[bufp++]=c;
}
When ungetch() is executed, a test is made to determine if
this store would exceed the buffer length. If bufp will be greater
that BUFSIZE after it is incremented, an error return is executed.
Otherwise, the return character is put in the buffer and it is there to
be read the next time getch() is executed.
This program has introduced several C functions. In fact, C has a
ungetchar() that does the same function as ungetch() above. It
has given examples of structures containing pointers to like structures,
recursive functions to process data, and dynamic memory management.
EXERCISES
1. With an editor, enter the tree program and compile it. To run this
program, use an input redirection such as c:>tree < (filename)
2. Modify the above program to determine the number of levels, and
record the level number with each word. Print out the levels when
the output is printed. Hint: the levels can be determined in the
addtree() function, and the printout of the levels require a modi
fication of treeprint().
3. Add a histogram to the above program that shows the number of
entries in each level. Count the total number of different words in
the document tested.