home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP9_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
32KB
|
719 lines
Chapter 9 - Standard Input/Output
THE STDIO.H HEADER FILE
Load the file SIMPLEIO.C for our first look at a file
with standard I/O. Standard I/O refers to the most usual
places where data is either read from, the keyboard, or
written to, the video monitor. Since they are used so much,
they are used as the default I/O devices and do not need to
be named in the Input/Output instructions. This will make
more sense when we actually start to use them so lets look
at the file in front of you.
The first thing you will notice is the second line of
the file, the #include "stdio.h" line. This is very much
like the #define we have already studied, except that
instead of a simple substitution, an entire file is read in
at this point. The system will find the file named
"stdio.h" and read its entire contents in, replacing this
statement. Obviously then, the file named "stdio.h" must
contain valid C source statements that can be compiled as
part of a program. This particular file is composed of
several standard #defines to define some of the standard I/O
operations. The file is called a header file and you will
find several different header files on the source disks that
came with your C compiler. Each of the header files has a
specific purpose and any or all of them can be included in
any program.
Your C compiler uses the double quote marks to indicate
that the search for the "include" file will begin in the
current directory, and if it not found there, the search
will continue in the "include" directory as set up in the
environment. It also uses the "less than" and "greater
than" signs to indicate that the file search should begin in
the directory specified in the environment. Most of the
programs in this tutorial have the double quotes in the
"include" statements. The next program uses the "<" and ">"
to illustrate the usage. Note that this will result is a
slightly faster (but probably unnoticeable) compilation
because the system will not bother to search the current
directory.
INPUT/OUTPUT OPERATIONS IN C
Actually the C programming language has no input or
output operations defined as part of the language, they must
be user defined. Since everybody does not want to reinvent
his own input and output operations, the compiler writers
have done a lot of this for us and supplied us with several
input functions and several output functions to aid in our
program development. The functions have become a standard,
and you will find the same functions available in nearly
Page 59
Chapter 9 - Standard Input/Output
every compiler. In fact, the industry standard of the C
language definition has become the book written by Kernigan
and Ritchie, and they have included these functions in their
definition. You will often, when reading literature about
C, find a reference to K & R. This refers to the book, "The
C Programming Language", written by Kernigan and Ritchie.
You would be advised to purchase a copy for reference.
You should print out the file named "stdio.h" and spend
some time studying it. There will be a lot that you will
not understand about it, but parts of it will look familiar.
The name "stdio.h" is sort of cryptic for "standard
input/output header", because that is exactly what it does.
It defines the standard input and output functions in the
form of #defines and macros. Don't worry too much about the
details of this now. You can always return to this topic
later for more study if it interests you, but you will
really have no need to completely understand the "stdio.h"
file. You will have a tremendous need to use it however, so
these comments on its use and purpose are necessary.
OTHER INCLUDE FILES
When you begin writing larger programs and splitting
them up into separately compiled portions, you will have
occasion to use some statements common to each of the
portions. It would be to your advantage to make a separate
file containing the statements and use the #include to
insert it into each of the files. If you want to change any
of the common statements, you will only need to change one
file and you will be assured of having all of the common
statements agree. This is getting a little ahead of
ourselves but you now have an idea how the #include
directive can be used.
BACK TO THE FILE NAMED "SIMPLEIO.C"
Lets continue our tour of the file in question. The
one variable "c" is defined and a message is printed out
with the familiar "printf" function. We then find ourselves
in a continuous loop as long as "c" is not equal to capital
X. If there is any question in your mind about the loop
control, you should review chapter 3 before continuing. The
two new functions within the loop are of paramount interest
in this program since they are the new functions. These are
functions to read a character from the keyboard and display
it on the monitor one character at a time.
The function "getchar()" reads a single character from
the standard input device, the keyboard being assumed
because that is the standard input device, and assigns it to
Page 60
Chapter 9 - Standard Input/Output
the variable "c". The next function "putchar(c)", uses the
standard output device, the video monitor, and outputs the
character contained in the variable "c". The character is
output at the current cursor location and the cursor is
advanced one space for the next character. The system is
therefore taking care of a lot of the overhead for us. The
loop continues reading and displaying characters until we
type a capital X which terminates the loop.
Compile and run this program for a few surprises. When
you type on the keyboard, you will notice that what you type
is displayed faithfully on the screen, and when you hit the
return key, the entire line is repeated. In fact, we only
told it to output each character once but it seems to be
saving the characters up and redisplaying them. A short
explanation is in order.
DOS IS HELPING US OUT (OR GETTING IN THE WAY)
We need to understand a little bit about how DOS works
to understand what is happening here. When data is read
from the keyboard, under DOS control, the characters are
stored in a buffer until a carriage return is entered at
which time the entire string of characters is given to the
program. When the characters are being typed, however, the
characters are displayed one at a time on the monitor. This
is called echo, and happens in many of the applications you
run.
With the above paragraph in mind, it should be clear
that when you are typing a line of dat