Format Specification Fields


Home ] Su ]

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:

  • White-space characters: blank (' '); tab ('\t'); or newline ('\n'). A white-space character causes the block to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.
  • Non–white-space characters, except for the percent sign (%). A non–white-space character causes the block to read, but not store, a matching non–white-space character. If the next character in the input buffer does not match, the block terminates the read, sending an error.
  • Format specifications, introduced by the percent sign (%). A format specification causes the block to read and convert characters in the input into values of a specified type. The value is assigned to an argument in the argument list.

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, %d). The percent sing is always intended as a format-control character, except in the case that another percent sing follows. Then, to specify that a percent-sign character is to be input, use %%

Format

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

Character Type of Input Expected
d Decimal integer.
i Decimal, hexadecimal, or octal integer.
o Octal integer.
x Hexadecimal integer.
e, E, f, g, G Floating-point value consisting of optional sign (+ or –), series of one or more decimal digits containing decimal point, and optional exponent (“e” or “E”) followed by an optionally signed integer value.

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

To Specify Use Prefix With Type Specifier
double l e, E, f, g, or G
long int l d, i, o, x, or X

This page has been updated on day 10/09/04.

Leonardo Daga's Warehouseâ, http://leonardodaga.insyde.it
Send any Comments to: leonardo.daga@gmail.com