Scanf format string

A scanf format string ("scan f"ormatted) is a control parameter used in various functions to specify the layout of an input string. The functions can then divide the string and translate into values of appropriate data types. String scanning functions are often supplied in standard libraries.
The term "scanf" comes from the C library, which popularized this type of function, but such functions predate C, and other names are used, such as codice_1 in ALGOL 68. scanf format strings, which provide formatted input (parsing), are complementary to printf format strings, which provide formatted output (templating). These provide simple functionality and fixed format compared to more sophisticated and flexible parsers or template engines, but are sufficient for many purposes.
History.
Mike Lesk's portable input/output library, including codice_2, officially became part of Unix in Version 7.
Usage.
The codice_2 function, which is found in C, reads input for numbers and other datatypes from standard input (often a command line interface or similar kind of a text user interface).
The following C code reads a variable number of unformatted decimal integers from the standard input stream and prints each of them out on separate lines:
int main(void)
 int n;
 while (scanf("%d", &n) == 1)
 printf("%d\n", n);
 return 0;
After being processed by the program above, an irregularly spaced list of integers such as
 456 123 789 456 12
 456 1
 2378
will appear consistently spaced as:
 456
 123
 789
 456
 12
 456
 1
 2378
To print out a word:
int main(void)
 char word[20];
 if (scanf("%19s", word) == 1)
 puts(word);
 return 0;
No matter what the data type the programmer wants the program to read, the arguments (such as codice_4 above) must be pointers pointing to memory. Otherwise, the function will not perform correctly because it will be attempting to overwrite the wrong sections of memory, rather than pointing to the memory location of the variable you are attempting to get input for.
In the last example an address-of operator (codice_5) is "not" used for the argument: as codice_6 is the name of an array of codice_7, as such it is (in all contexts in which it evaluates to an address) equivalent to a pointer to the first element of the array. While the expression codice_8 would numerically evaluate to the same value, semantically, it has an entirely different meaning in that it stands for the address of the whole array rather than an element of it. This fact needs to be kept in mind when assigning codice_2 output to strings.
As codice_2 is designated to read only from standard input, many programming languages with interfaces, such as PHP, have derivatives such as codice_11 and codice_12 but not codice_2 itself.
Format string specifications.
The formatting placeholders in codice_2 are more or less the same as that in codice_15, its reverse function. As in printf, the POSIX extension is defined.
There are rarely constants (i.e., characters that are not formatting placeholders) in a format string, mainly because a program is usually not designed to read known data, although codice_2 does accept these if explicitly specified. The exception is one or more whitespace characters, which discards all whitespace characters in the input.
Some of the most commonly used placeholders follow:
The above can be used in compound with numeric modifiers and the codice_51, codice_52 modifiers which stand for "long" and "long long" in between the percent symbol and the letter. There can also be numeric values between the percent symbol and the letters, preceding the codice_53 modifiers if any, that specifies the number of characters to be scanned. An optional asterisk (codice_54) right after the percent symbol denotes that the datum read by this format specifier is not to be stored in a variable. No argument behind the format string should be included for this dropped variable.
The codice_55 modifier in printf is not present in scanf, causing differences between modes of input and output. The codice_56 and codice_29 modifiers are not present in the C90 standard, but are present in the C99 standard.
An example of a format string is
The above format string scans the first seven characters as a decimal integer, then reads the remaining as a string until a space, newline, or tab is found, then consumes whitespace until the first non-whitespace character is found, then consumes that character, and finally scans the remaining characters as a double. Therefore, a robust program must check whether the codice_2 call succeeded and take appropriate action. If the input was not in the correct format, the erroneous data will still be on the input stream and must discarded before new input can be read. An alternative method, which avoids this, is to use codice_60 and then examine the string read in. The last step can be done by codice_11, for example.
In the case of the many float type characters , many implementations choose to collapse most into the same parser. Microsoft MSVCRT does it with , while glibc does so with all four.
Vulnerabilities.
codice_2 is vulnerable to format string attacks. Great care should be taken to ensure that the formatting string includes limitations for string and array sizes. In most cases the input string size from a user is arbitrary and cannot be determined before the codice_2 function is executed. This means that uses of codice_46 placeholders without length specifiers are inherently insecure and exploitable for buffer overflows. Another potential problem is to allow dynamic formatting strings, for example formatting strings stored in configuration files or other user-controlled files. In this case the allowed input length of string sizes cannot be specified unless the formatting string is checked beforehand and limitations are enforced. Related to this are additional or mismatched formatting placeholders which do not match the actual vararg list. These placeholders might be partially extracted from the stack or contain undesirable or even insecure pointers, depending on the particular implementation of varargs.