Sams Teach Yourself C in 21 Days

(singke) #1
Working with the Screen, Printer, and Keyboard 351

14


sequence of non-whitespace characters that ends when the next white space is encoun-
tered or when the field width, if specified, is reached. The conversion specification com-
ponents include the following:


  • The optional assignment suppression flag (*) immediately follows the %. If present,
    this character tells scanf()to perform the conversion corresponding to the current
    conversion specifier but to ignore the result (not assign it to any variable).

  • The next component, the field width, is also optional. The field width is a decimal
    number specifying the width, in characters, of the input field. In other words, the
    field width specifies how many characters from stdin scanf()should examine for
    the current conversion. If a field width isn’t specified, the input field extends to the
    next white space.

  • The next component is the optional precision modifier, a single character that can
    beh,l,orL. If present, the precision modifier changes the meaning of the type
    specifier that follows it. Details are given later in this chapter.

  • The only required component of the conversion specifier (besides the %) is the type
    specifier. The type specifier is one or more characters that tell scanf()how to
    interpret the input. These characters are listed and described in Table 14.3. The
    Argument column lists the required type of the corresponding variable. For exam-
    ple, the type specifier drequiresint *(a pointer to type int).


TABLE14.3 The type specifier characters used in scanf()conversion specifiers
Type Argument Meaning of Type
d int * A decimal integer.
i int * An integer in decimal, octal (with leading 0 ), or hexadeci-
mal (with leading 0Xor0x) notation.
o int * An integer in octal notation with or without the leading 0.
u unsigned int * An unsigned decimal integer.
x int * A hexadecimal integer with or without the leading 0Xor0x.
c char * One or more characters are read and assigned sequentially
to the memory location indicated by the argument. No ter-
minating\0is added. If a field width argument isn’t given,
one character is read. If a field width argument is given,
that number of characters, including white space (if any), is
read.
s char * A string of non-whitespace characters is read into the spec-
ified memory location, and a terminating \0is added.

22 448201x-CH14 8/13/02 11:12 AM Page 351

Free download pdf