A scanf
template string is a string that contains ordinary multibyte characters interspersed with conversion specifications that start with `%'.
Any whitespace character (as defined by the isspace
function; see Classification of Characters) in the template causes any number of whitespace characters in the input stream to be read and discarded. The whitespace characters that are matched need not be exactly the same whitespace characters that appear in the template string. For example, write `, ' in the template to recognize a comma with optional whitespace before and after.
Other characters in the template string that are not part of conversion specifications must match characters in the input stream exactly; if this is not the case, a matching failure occurs.
The conversion specifications in a scanf
template string have the general form:
% flags width type conversion
In more detail, an input conversion specification consists of an initial `%' character followed in sequence by:
An optional flag character `*', which says to ignore the text read for this specification. When scanf
finds a conversion specification that uses this flag, it reads input as directed by the rest of the conversion specification, but it discards this input, does not use a pointer argument, and does not increment the count of successful assignments.
An optional flag character `a' (valid with string conversions only) which requests allocation of a buffer long enough to store the string in. (This is a GNU extension.) See Dynamic String Input.
An optional decimal integer that specifies the maximum field width. Reading of characters from the input stream stops either when this maximum is reached or when a non-matching character is found, whichever happens first. Most conversions discard initial whitespace characters (those that don't are explicitly documented), and these discarded characters don't count towards the maximum field width. String input conversions store a null character to mark the end of the input; the maximum field width does not include this terminator.
An optional type modifier character. For example, you can specify a type modifier of `l' with integer conversions such as `%d' to specify that the argument is a pointer to a long int
rather than a pointer to an int
.
A character that specifies the conversion to be applied.
The exact options that are permitted and how they are interpreted vary between the different conversion specifiers. See the descriptions of the individual conversions for information about the particular options that they allow.
With the `-Wformat' option, the GNU C compiler checks calls to scanf
and related functions. It examines the format string and verifies that the correct number and types of arguments are supplied. There is also a GNU C syntax to tell the compiler that a function you write uses a scanf
-style format string. See Declaring Attributes of Functions, for more information.