C18. C Scanf Function


The scanf Function
Just as printf prints output in a specified format, scanf reads input according to a particular format. A scanf format string, like a printf format string, may contain both ordinary characters and conversion specifications. The conversions allowed with scanf are essentially the same as those used with printf.

In many cases, a scanf format string will contain only conversion specifica­tions, as in the following example:
int i j ; float x, y;
scanf(“%d%d%f%f”, &i, &j, &x, &y); Suppose that the user enters the following input line: 1 -20 .3 -4.0e3
scanf will read the line, converting its characters to the numbers they represent, and then assign 1, -20, 0.3, and -4000.0 to i, j. x, and y, respectively. “Tightly packed” format strings like “ %d%d%f %f” are common in scanf calls, printf format strings are less likely to have adjacent conversion specifications.

scanf, like printf, contains several traps for the unwary. When using scanf. the programmer must check that the number of conversion specifications matches the number of input variables and that each conversion is appropriate for the corresponding variable  >as with printf, the compiler isn't required to check for a possible mismatch. Another trap involves the & symbol, which normally pre­cedes each variable in a scanf call. The & is usually (but not always) required, and it's the programmer's responsibility to remember to use it.

Calling scanf is a powerful but unforgiving way to read data. Many profes­sional C programmers avoid scanf, instead reading all data in character form and converting it to numeric form later. We'll use scanf quite a bit. especially in the early beginiers because it provides a simple way to read numbers. Be aware, however, that many of our programs won't behave properly if the user enters unexpected input. As we'll see later, it's possible to have a program test whether scanf successfully read the requested data (and attempt to recover if it didn't). Such tests are impractical for the programs in this book  >they would add too many statements and obscure the point of the examples.
How scanf Works
scanf can actually do much more than I've indicated so far. It is essentially a “pattern-matching” function that tries to match up groups of input characters with conversion specifications.

Like the printf function, scanf is controlled by the format string. When it is called, scanf begins processing the information in the string, starting at the left. For each conversion specification in the format string, scanf tries to locate an item of the appropriate type in the input data, skipping blank space if necessary, scanf then reads the item, stopping when it encounters a character that can't pos­sibly belong to the item. If the item was read successfully, scanf continues pro­cessing the rest of the format string. If any item is not read successfully, scanf returns immediately without looking at the rest of the format string (or the remain­ing input data).

As it searches for the beginning of a number, scanf ignores white-space characters (the space, horizontal and vertical tab. form-feed, and new-line charac­ters). As a result, numbers can be put on a single line or spread out over several lines. 

No comments: