Previous: 8.2.1 Formatted Output: printf()
Up: 8.2 Formatted Input/Output
Previous Page: 8.2.1 Formatted Output: printf()
Next Page: 8.3 Direct I/O with Files

8.2.2 Formatted Input: scanf()

Like printf(), scanf() expects its first argument to be a format string, but unlike printf(), the remaining arguments are addresses of the variables in which to put the data that is read. The prototype for scanf(), also in stdio.h, is:

int scanf(char *, ...);
As we've said, the returned value is the number of items read, or EOF. The format string controls the input order, conversion of the input data to the specified type, and format specification. Each conversion specification appearing in the format string is applied, in turn, to the next input data item in the input stream. After the specified conversion, the item is placed where the next succeeding argument points; so each of the arguments must be an address.

Besides the conversion specifications that start with %, the format string may also include regular characters. Regular white space characters in the format string are ignored. Any regular non-white space characters must be matched exactly in the input stream. For example:

scanf("x= %d", &x);
The input stream must include the characters x=, which are matched by the corresponding character in the format string, before an integer value is read. A valid sample input for this format string is: The characters, x=, are first matched, then the integer, 1234, is read and assigned to the variable, x. If the characters, x=, are not matched in the input stream, no input is possible, and scanf() will return the value 0.

As before, a conversion specification starts with a % and ends with one of the conversion characters given in Table 8.2. Between % and the conversion character, there can be an optional assignment suppression character, *, followed by an optional number indicating the maximum field width. The maximum field width specifies that no more than that number of characters in the input stream may be used for the next data item. The converted result is stored where the corresponding argument points unless the assignment suppression character is used. If the suppression character is used, the result is discarded. The conversion characters with their meanings are given in Table 8.2.

All of these except c and s may be preceded by the length modifier, l (ell), where, in the case of integral type data, the corresponding argument should be long and in the case of floating point data, the argument should be double. For example, with the following declarations:

int i, k;
    char c;
    float f1;
    double d1;
    char s[80];
    long x;
Consider the following statements with the input as shown below each statement.
scanf("Integer: %4d %f", &i, &f1);

Input is:

First, the regular characters, Integer: are matched. Then a field of 4 is used to read the integer, 1234. Finally, a float, 567.0 is read. The integer, 1234, will be stored in i, and 567.0 will be stored in f1.
scanf("%4s %*c %c", s, &c);
A field of 4 is used to read a string, "Surp", which is placed in s. The next character, 'r', will be read and discarded, and the next character, 'i', will be stored in c.
scanf("%s %*s %d", s, &i);
  • Surprise number 1
This time the string "Surprise" will be stored in s, the next string "number" will be discarded, and the integer 1 will be stored in i.



Previous: 8.2.1 Formatted Output: printf()
Up: 8.2 Formatted Input/Output
Previous Page: 8.2.1 Formatted Output: printf()
Next Page: 8.3 Direct I/O with Files

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:15:23 HST 1994