Chapter 4 Data Input and Output
Chapter 4 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
3
SINGLE CHARACTER INPUT -THE getchar FUNCTION
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)
6
7
Contd..
8
ENTERING INPUT DATA -THE scanf FUNCTION
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..
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
23
Example
24
Example
26
Example
28
MORE ABOUT THE printf FUNCTION
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..
31
Commonly Used Flags
32
THE gets AND puts FUNCTIONS
33
Example
34
INTERACTIVE (CONVERSATIONAL) PROGRAMMING
35
Example
36
Example
37
Contd..
38
Thank You!!
39