How I Did Character
Input and Output
We are going to
consider a family of related programs for processing character data. You will
find that many programs are just expanded versions of the prototypes that we
discuss here.
The model of input and output supported by the
standard library is very simple. Text input or output, regardless of where it
originates or where it goes to, is dealt with as streams of characters.
A text
stream is a sequence of characters divided into lines; each line consists
of zero or more characters followed by a newline character. It is the
responsibility of the library to make each input or output stream confirm this
model; the C programmer using the library need not worry about how lines are
represented outside the program.
The standard library provides several
functions for reading or writing one character at a time, of which getchar and
putchar are the simplest. Each time it is called, getchar reads the next
input character from a text stream and returns that as its value. That is,
after
c = getchar();
the variable c contains the next character of input. The
characters normally come from the keyboard; input from files is discussed in Input and Output
The function putchar prints a character each time it is
called:
putchar(c);
prints the contents of the integer variable c as a
character, usually on the screen. Calls to putchar and printf may be
interleaved; the output will appear in the order in which the calls are made.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment