Programming in C

(Barry) #1

208 Chapter 10 Character Strings


In the preceding program, the scanffunction is called to read in three character strings:
s1,s2,and s3. Because the first line of text contains only two character strings—where
the definition of a character string to scanfis a sequence of characters up to a space,
tab, or the end of the line—the program waits for more text to be entered. After this is
done, the printfcall is used to verify that the strings "system","expansion", and
"bus"are correctly stored inside the string arrays s1,s2,and s3,respectively.
If you type in more than 80 consecutive characters to the preceding program without
pressing the spacebar, the tab key, or the Enter (or Return) key,scanfoverflows one of
the character arrays.This might cause the program to terminate abnormally or cause
unpredictable things to happen. Unfortunately,scanfhas no way of knowing how large
your character arrays are.When handed a %sformat, it simply continues to read and
store characters until one of the noted terminator characters is reached.
If you place a number after the %in the scanfformat string, this tells scanfthe max-
imum number of characters to read. So, if you used the following scanfcall:
scanf ("%80s%80s%80s", s1, s2, s3);
instead of the one shown in Program 10.5,scanfknows that no more than 80 charac-
ters are to be read and stored into either s1,s2, or s3.(You still have to leave room for
the terminating null character thatscanfstores at the end of the array.That’s why %80s
is used instead of %81s.)

Single-Character Input


The standard library provides several functions for the express purposes of reading and
writing single characters and entire character strings. A function called getcharcan be
used to read in a single character from the terminal. Repeated calls to the getcharfunc-
tion return successive single characters from the input.When the end of the line is
reached, the function returns the newline character '\n'.So, if the characters “abc” are
typed at the terminal, followed immediately by the Enter (or Return) key, the first call to
the getcharfunction returns the character 'a', the second call returns the character
'b', the third call returns 'c', and the fourth call returns the newline character '\n'.A
fifth call to this function causes the program to wait for more input to be entered from
the terminal.
You might be wondering why you need the getcharfunction when you already
know how to read in a single character with the %cformat characters of the scanffunc-
tion. Using the scanffunction for this purpose is a perfectly valid approach; however,
the getcharfunction is a more direct approach because its sole purpose is for reading in
single characters, and, therefore, it does not require any arguments.The function returns
a single character that might be assigned to a variable or used as desired by the program.
In many text-processing applications, you need to read in an entire line of text.This
line of text is frequently stored in a single place—generally called a “buffer”—where it is
processed further. Using the scanfcall with the %sformat characters does not work in
such a case because the string is terminated as soon as a space is encountered in the
input.
Free download pdf