Chapter 4 Summary and Quiz
Recap of character and formatted I/O.
The Console, Mastered
Input and output looked solved after chapter 1; this chapter showed how much was underneath. You now know the stream model that all four functions share, the format language that shapes every printed character, and the return value that tells the truth about input. The recap, tool by tool.
The Stream Model
Input is a stream of characters, newlines included, consumed strictly in order. Whatever a read does not consume stays for the next read. End of the stream is a real event, reported as EOF, a negative <stdio.h> constant. These four sentences explain nearly every I/O surprise in C.
Character I/O
getchar() returns the next character or EOF, and its result therefore lives in an int: on this platform char is unsigned, and a char-typed comparison against EOF can simply never be true. putchar(c) writes one character. Character arithmetic works directly on what you read: ch - 'a' + 'A' uppercases, ch - '0' converts a digit character to its value.
<ctype.h> is the portable way to ask what a character is: isalpha, isdigit, isalnum, islower, isupper, isspace, ispunct, and the rest. They return nonzero, never a guaranteed 1, so no result of theirs should be compared with == 1. Their argument must be EOF or fit in an unsigned char, which is exactly getchar's range and the second reason the value belongs in an int. toupper and tolower pass anything that is not a letter straight through, so they need no test in front of them; the arithmetic form does. The read-until-EOF while loop is previewed and waits for chapter 6 to become fully yours.
printf's Format Language
A conversion spec is %[flags][width][.precision]specifier. Width is a minimum field, right-justified and space-padded unless the - flag (left) or 0 flag (zero-fill) says otherwise; overflow never truncates. Precision on %f sets decimals and rounds, while precision on %s does the opposite job and truncates to that many characters with nothing in the output to show it. Two more flags: + forces a sign onto a positive number and a space reserves the column instead, and # supplies the 0 and 0x that make %o and %x unambiguous and keeps the decimal point on %e, %f, and %g. %g itself picks whichever of %f and %e is shorter, drops trailing zeros, and counts significant digits rather than decimals. Specifier and argument type must match, %d for int, %f for double, %lu for unsigned long, because a mismatch is undefined behaviour, not a formatting choice. %-10s beside %8.2f is the whole secret of aligned tables.
scanf's Contract
Each conversion skips leading whitespace (except %c and %[...]), consumes what matches, and stops, leaving the rest, trailing newline included, in the stream. Whitespace in the format string means "skip whitespace", giving the " %c" idiom that dodges leftover newlines. The return value counts successful conversions, with EOF meaning the stream ended first; failed conversions leave variables untouched and the bad characters in place. Initialization plus the count give a program a defined state no matter what was typed.
Three parts of the format string decide how much gets read. A width caps a conversion, so %2d against 6789 yields 67 and leaves 89 for whoever reads next, which is how one wrong width shifts every later value along. A * reads a field and assigns nothing, and because the return value counts assignments, a suppressed field never appears in it. And anything that is neither a conversion nor whitespace is a literal the input must supply: "%d-%d" demands its hyphen, and a space where the hyphen should be is a matching failure that stops the call on the spot. %[...] and %[^\n] let you spell the acceptable character set out yourself, with the width mandatory (one less than the array size), no leading-whitespace skip, and an empty match counting as failure. Chapter 8 replaces the last of these with fgets, which bounds the read by construction.
One Program, Whole Chapter
Feed it 3 4.5 and on the next line k:
conversions: 2
items 3
price 4.50
K
The first getchar collects the leftover newline that %lf stopped at, the second the real character, the table aligns by width and flag, and toupper capitalizes an initial it has not had to verify is a letter: every mechanism of the chapter in one run. Trace it, run it, take the quiz.
Looking Ahead
Chapter 5 gives programs their first real decisions: if in all its forms, switch, and finally the ability to act on everything the return values have been telling you.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Chapter 4 Summary and Quiz - Quiz
Test your understanding of the lesson.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!