UMBC CMSC201, Computer Science I, Fall 1994
Sections 0101, 0102 and Honors
Thursday December 8, 1994
Assigned Reading: 16.1 - 16.6
Handouts (available on-line): none
Topics Covered:
- Attendance was taken.
- Discussed the difference between using pointers to
access the elements of an array by dereferencing and
by array notation. For example, if p is
a pointer to integers and A is an array of
integers, then after the assignment p = A,
we can access the first element of A using
*p or p[0]. If we intend to
use p as an array, then it is much clearer
to use p[0]. Some people argue that this
clearer method may be less efficient. However,
any modern optimizing compiler should be able to
generate code that removes these inefficiencies.
So, we should opt for clarity.
- Speaking of clarity, we have
a very unclear program
which produces an interesting output.
Unfortunately the author of the program misspelled "eighth" as
"eigth" in this program. Just imagine having to figure out
how to correct the program!
- We looked at the standard C string library. A complete
description of these functions can be found in
the manual pages
on UNIX by issuing the man string command.
The functions in the standard string library offer
some of the same capabilities as the course library, but
do not check for errors as rigorously as the string functions
from the course library. Also, to use the string functions
in the standard C library, you will have to allocate space
for the strings yourself. We looked at the source code for
some of the string functions in the course library
strilib.c. We discussed
how the functions: IthChar, Concat,
StringLength, SubString and CreateString
work. Competent programmers should avoid using functions
like IthChar, StringLength and Concat
because they are much slower than the standard routines.
- Next, we looked at how the function scanf from
the stdio.h interface works. This is the function
you would use if you did not use GetInteger, and
GetReal from the course library. The function
scanf takes pointers to variables as parameters
(parameters passed by reference) so it can store the user's input
in those variables.
- Our first program using
scanf simply reads in an integer and a double
and prints them out. Sample run.
- Our second example shows
the consequence of forgetting the & in front
of the variable passed to scanf: you get a core dump.
- Our third example shows
two alternate forms of scanf called sscanf
and fscanf. These functions respectively read the
input from a given string or a file instead of the keyboard.
- Our fourth example shows
that scanf can be used to read in several integers
per line. The value returned by scanf is actually
the number of variables it was able to fill in, and not
the value stored in the variable. If scanf encounters
the end-of-file character (control-D on UNIX), then it returns
a -1. Sample runs.
- Our fifth example shows that
using scanf to read in several variables can be confusing
to the user. In this program, we try to read in an integer
followed by a double. The results could be confusing to
the user because it is not always clear where the integer ends.
- Now that we have seen how scanf works we can look
at how GetInteger is implemented in the source code
of the simpio library.
We also examined the strategy used for the GetLine
function that allows us to read in a string without setting
a limit on the length of the input.
Last Modified:
Wed Dec 14 10:04:59 EST 1994
Richard Chang, chang@gl.umbc.edu