Character I/O
Textbook Background
The C standard library gives specialized means to read characters and strings, explicitly, as described in the following textbook sections.
- King: Section 3.1, pages 37-41, OR
- Kernighan & Ritchie: 1.5, 7.1-7.2, B.2 (reference)
Readings Characters and Strings
C provides two equivalent approaches for reading individual characters.
-
The C function
getchar()reads and returns a single character. For example:char ch; ch = getchar(); -
The
cformat forscanfreads an individual character. For example:char ch; scanf("%c", &ch);
When reading character data with either getchar
or %c with scanf, the first character is read and
recorded; that is, the process of reading a character does NOT skip
over white space.
Some of you may have noticed that Kernighan & Ritchie's
book, The C Programming Language, declares int
variables and sets them to character values, while the above examples
declare char variables set to character values. The local
system automatically converts the int values
that getchar and getc
into char values when set to a char
variable, so on the local system, these declarations are valid. Just
be aware that some systems may not support this automatic conversion.
Reading Strings
C also provides at least three approaches for reading strings of characters. Each function has its own special characteristics.
-
The
sformat forscanfreads a sequence of non-white-space characters. As with all strings, a null character ('\0') is added at the end of the string. For example:char str[10]; /* allow room for 10 characters, including the null */ scanf("%s", str); /* since str is an array, the variable represents a base address and no ampersand & is added */ -
The C function
gets()reads an entire line or until end of file. As withscanf, a null character is added at the end of the string. For example:char str[10]; gets(str); -
The C function
fgets()reads up toncharacters from a line or until end of file. As withscanf, a null character is added at the end of the string. For example:
The needed length of a string may change; use the same constant value throughout the code is dangerously poor practice. A better way to proceed in cases like this is to declare a constant to hold the desired length. For example:char str[10]; /* stdin is the C variable for "standard input" */ fgets(str, 10, stdin); /* up to 9 characters are read from the terminal, leaving room for a null character at the end */const int bufsz = 10; char str[bufsz]; /* stdin is the C variable for "standard input" */ fgets(str, bufsz, stdin); /* up to bufsz-1 characters are read from the terminal, leaving room for a null character at the end */
As with reading character data, stored input starts immediately with the first character read; the process of reading a string does NOT skip over white space.
Warning: Both scanf and gets
read characters (until white space or the end of a line), without
regard for the size of the string array. If more characters are read
than fit in the array, the characters may overflow memory to fill data
stored in other variables. Thus, only
fgets and getchar can be considered safe.
