Template:cpp/io/c/scanf format

The format string consists of
 * non-whitespace characters except : each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal.
 * whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling in a loop). Note that there is no difference between, , , or other whitespace in the format string.
 * conversion specifications. Each conversion specification has the following format:


 * introductory character


 * assignment-suppressing character . If this option is present, the function does not assign the result of the conversion to any receiving argument.


 * integer number (greater than zero) that specifies maximum field width, that is, the maximum number of characters that the function is allowed to consume when doing the conversion specified by the current conversion specification. Note that and  may lead to buffer overflow if the width is not provided.


 * length modifier that specifies the size of the receiving argument, that is, the actual destination type. This affects the conversion accuracy and overflow rules. The default destination type is different for each conversion type (see table below).


 * conversion format specifier

The following format specifiers are available:

For every conversion specifier other than, the longest sequence of input characters which does not exceed any specified ﬁeld width and which either is exactly what the conversion specifier expects or is a prefix of a sequence it would expect, is what's consumed from the stream. The ﬁrst character, if any, after this consumed sequence remains unread. If the consumed sequence has length zero or if the consumed sequence cannot be converted as specified above, the matching failure occurs unless end-of-ﬁle, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.

All conversion specifiers other than, , and consume and discard all leading whitespace characters (determined as if by calling ) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width.

The conversion specifiers and  always store the null terminator in addition to the matched characters. The size of the destination array must be at least one greater than the specified field width. The use of or, without specifying the destination array size, is as unsafe as.

The correct conversion specifications for the fixed-width integer types (int8_t, etc) are defined in the header (although, , etc is synonymous with , , etc).

There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.

When parsing an incomplete floating-point value that ends in the exponent with no digits, such as parsing with the conversion specifier, the sequence  (the longest prefix of a possibly valid floating-point number) is consumed, resulting in a matching error (the consumed sequence cannot be converted to a floating-point number), with  remaining. Some existing implementations do not follow this rule and roll back to consume only, leaving , e.g. glibc bug 1765