Data Input and Output
Chapter 4
1
Introduction
We have already seen that the C language is accompanied by
a collection of library functions, which includes a number of
input/output functions.
In this chapter we will make use of six of these functions:
getchar, putchar, scanf, printf , gets and puts.
These six functions permit the transfer of information between
the computer and the standard input/output devices (e.g., a
keyboard and a TV monitor).
The first two functions, getchar and putchar, allow single
characters to be transferred into and out of the computer;
scanf and printf are the most complicated, but they permit the
transfer of single characters, numerical values and strings;
gets and puts facilitate the input and output of strings.
Once we have learned how to use these functions, we will be
able
2 to write a number of complete, though simple, C
Example
Here is an outline of a typical C program that
makes use of several input/output routines from the
standard C library.
3
SINGLE CHARACTER INPUT -THE getchar FUNCTION
The getchar function is a part of the standard C
I/O library. It returns a single character from a
standard input device (typically a keyboard).
character variable = getchar( );
where character variable refers to some previously
declared character variable.
4
Contd..
If an end-of-file condition is encountered when
reading a character with the getchar function, the
value of the symbolic constant EOF will
automatically be returned. (This value will be
assigned within the stdio .h file. Typically, EOF will
be assigned the value -1, though this may vary
from one compiler to another.)
The detection of EOF in this manner offers a
convenient way to detect an end of file, whenever
and wherever it may occur.
The getchar function can also be used to read
multicharacter strings, by reading one character at
a time
5 within a multipass loop.
SINGLE CHARACTER OUTPUT -THE putchar FUNCTION
The putchar function, like getchar, is a part of the
standard C I/O library. It transmits a single
character to a standard output device (typically a
TV monitor).
putchar ( character variable)
where character variable refers to some previously
declared character variable.
The putchar function can be used to output a
string constant by storing the string within a one
dimensional, character-type array.
6
7
Contd..
8
ENTERING INPUT DATA -THE scanf FUNCTION
This function can be used to enter any combination
of numerical values, single characters and strings.
The function returns the number of data items that
have been entered successfully.
In general terms, the scanf function is written as
scanf(contro1 string, argl, arg2, . . . , argn)
where control string refers to a string containing
certain required formatting information, and
argl,arg2, . . . argn are arguments that represent
the individual input data items. (Actually, the
arguments represent pointers that indicate the
addresses of the data items within the computer's
memory.
9
Contd..
The control string consists of individual groups of
characters, with one character group for each input
data item.
Each character group must begin with a percent
sign (%). In its simplest form, a single character
group will consist of the percent sign, followed by a
conversion character which indicates the type of
the corresponding data item.
The use of blank spaces as character-group
separators is very common.
10
11
Contd..
The arguments are written as variables or arrays,
whose types match the corresponding character
groups in the control string. Each variable name
must be preceded by an ampersand (a). (The
arguments are actually pointers that indicate where
the data items are stored in the computer's
memory, as explained in). However, array names
should not begin with an ampersand.
12
Contd..
13
Contd..
If the control string begins by reading a character-
type data item, it is generally a good idea to
precede the first conversion character with a blank
space. This causes the scanf function to ignore any
extraneous characters that may have been entered
earlier.
14
Contd..
The s-type conversion character within the control
string is replaced by a sequence of characters
enclosed in square brackets, designated as [ . . . ].
Whitespace characters may be included within the
brackets, thus accommodating strings that contain
such characters.
A null character (\O)will then automatically be
added to the end of the string.
The order of the characters within the square
brackets need not correspond to the order of the
characters being entered.
Input characters may be repeated. The string will
terminate,
15 however, once an input character is
16
Contd..
A variation of this feature which is often more
useful is to precede the characters within the
square brackets by a Circumflex (i.e., ^).
Thus, when the program is executed, successive
characters will continue to be read from the
standard input device as long as each input
character does not match one of the characters
enclosed within the brackets.
17
MORE ABOUT THE scanf FUNCTION
The consecutive nonwhitespace characters that
define a data item collectively define afield. It is
possible to limit the number of such characters by
specifying a maximum field width for that data
item.
18
Contd…
In most versions of C it is possible to skip over a
data item, without assigning it to the designated
variable or array. To do so, the % sign within the
appropriate control group is followed by an asterisk
(*).
This feature is referred to as assignment
suppression.
19
Contd..
20
WRITING OUTPUT DATA -THE p r i n t f FUNCTION
This function can be used to output any
combination of numerical values, single characters
and strings.
It is similar to the input function scanf, except that
its purpose is to display data rather than to enter it
into the computer.
That is, the printf function moves data from the
computer’s memory to the standard output device,
whereas the scanf function enters data from the
standard input device and stores it in the
computer’s memory.
In general terms, the p r i n t f function is written as
printf(
21 control string, arg7, arg2, . . . , argn)
Contd..
In contrast to the scanf function discussed in the last
section, the arguments in a printf function do not represent
memory addresses and therefore are not preceded by
ampersands.
The control string consists of individual groups of characters,
with one character group for each output data item.
Each character group must begin with a percent sign (%).
In its simplest form, an individual character group will consist
of the percent sign, followed by a conversion character
indicating the type of the corresponding data item.
Multiple character groups can be contiguous, or they can be
separated by other characters, including whitespace
characters. These “other” characters are simply transferred
directly to the output device, where they are displayed. The
use22 of blank spaces as character-group separators is
Commonly Used Conversion Characters for Data
Output
23
Example
Executing the program produces the following
output:
2.000000 3.000000 5.000000 2.236068
24
Example
When the p r i n t f statement is executed, the
following output will be generated.
25fastener 12345 0.050000
Example
26
Example
A minimum field width can be specified by preceding
the conversion character by an unsigned integer.
This is just the opposite of the field width indicator in
the scanf function, which specifies a maximum field
width.
27
Example
28
MORE ABOUT THE printf FUNCTION
It is also possible to specify the maximum
number of decimal places for a floating-point
value, or the maximum number of characters
for a string. This specification is known as
precision. The precision is an unsigned
integer that is always preceded by a decimal
point.
29
Example
A minimum field width specification need not
necessarily accompany the precision specification.
It is possible to specify the precision without the
minimum field width, though the precision must still
be preceded by a decimal point.
30
Contd..
In addition to the field width, the precision and the
conversion character, each character group within
the control string can include a flag, which affects
the appearance of the output.
The flag must be placed immediately after the
percent sign (%)
Some compilers allow two or more flags to appear
consecutively, within the same character group.
31
Commonly Used Flags
32
THE gets AND puts FUNCTIONS
Before leaving this chapter, however, we mention
the gets and puts functions, which facilitate the
transfer of strings between the computer and the
standard input/output devices.
In the case of gets, the string will be entered fiom
the keyboard, and will terminate with a newline
character (i.e., the string will end when the user
presses the Enter key).
The string may include whitespace characters.
33
Example
34
INTERACTIVE (CONVERSATIONAL) PROGRAMMING
Many modern computer programs are designed to
create an interactive dialog between the computer
and the person using the program (the "user").
These dialogs usually involve some form of
question-answer interaction, where the computer
asks the questions and the user provides the
answers, or vice versa.
The computer and the user thus appear to be
carrying on some limited form of conversation.
In C, such dialogs can be created by alternate use
of the scanf and printf functions.
35
Example
36
Example
37
Contd..
38
Thank You!!
39