scanf Format Specifiers

From Appmethod Topics
Jump to: navigation, search

Go Up to scanf, wscanf


In scanf format strings, format specifiers have the following form:

% [*] [width] [F|N] [h|l|L] type_char

Each format specifier begins with the percent character (%).

After the % come the following, in this order:

Component   Optional/Required What It Is/Does

[*]

(Optional)

Precision character. The precision character is an asterisk (*), not to be confused with the C indirection (pointer) operator.

If the asterisk follows the percent sign (%) in a format specifier, the next input field will be scanned but it won't be assigned to the next address argument.

The suppressed input data is assumed to be of the type specified by the type character that follows the asterisk character.

[width]

(Optional)

Width specifier. The width specifier (n), a decimal integer, controls the maximum number of characters to be read from the current input field.

Up to n characters are read, converted, and stored in the current address argument. If the input field contains fewer than n characters, the ...scanf function reads all the characters in the field, then proceeds with the next field and format specifier.

The success of literal matches and suppressed assignments is not directly determinable.

If the ...scanf function encounters a whitespace or nonconvertible character before it reads "width" characters, it:

  • Reads, converts, and stores the characters read so far, then
  • Attends to the next format specifier.

A non-convertible character is one that cannot be converted according to the given format (8 or 9 when the format is octal, J or K when the format is hexadecimal or decimal, and so on).

[F|N]

(Optional)

Pointer size modifier. Overrides default size of address argument:

  • N=near pointer
  • F=far pointer

[h|l|L]

(Optional)

Argument-type modifier. Overrides default type of address argument:

  • h=short int
  • l=long int, if type_char specifies integer conversion
  • l=double, if type_char specifies floating-point conversion
  • L=long double (valid only with floating-point conversion)

type_char

(Required)

Type character


See Also