Format Specification Fields
Format Specification Fields
A format specification has the following form:
% [width] [l] type
The format argument specifies the interpretation of the input and can contain one or more of the following:
The format is read from left to right. Characters outside format specifications are expected to match the sequence of characters in input buffer; the matching characters in input buffer are scanned but not stored. If a character in input buffer conflicts with the format specification, the block terminates, and the character is left in input buffer as if it had not been read.
When the first format specification is encountered, the value of the first input field is converted according to this specification and stored in the location that is specified by the first argument. The second format specification causes the second input field to be converted and stored in the second argument, and so on through the end of the format string.
An input field is defined as all characters up to the first white-space character (space, tab, or newline), or up to the first character that cannot be converted according to the format specification, or until the field width (if specified) is reached. If there are too many arguments for the given specifications, the extra arguments are evaluated but ignored. The results are unpredictable if there are not enough arguments for the format specification.
Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a double, or an integer.
The simplest format specification contains only the percent sign and a type
character (for example,
The type character is the only required format field; it appears after any optional format fields. The type character determines whether the associated argument is interpreted as a character, string, or number.
Table 1 Type Characters for format scan functions
Format Width Specification
width is a positive decimal integer controlling the maximum number of characters to be read from input buffer. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters may be read if a white-space character (space, tab, or newline) or a character that cannot be converted according to the given format occurs before width is reached.
The optional prefixe l indicate the “size” of the argument (long or short, single-byte character or wide character, depending upon the type character that they modify). This format-specification character is used with type characters in format block functions to specify interpretation of arguments as shown in the Table 2. The type characters and their meanings are described in Table 1
Table 2 Size Prefixes for format block Format-Type Specifiers