Scanf format string refers to a control parameter used by a class of functions in the string-processing
libraries of various programming languages. The format string specifies a method for reading
a string into an arbitrary number of varied data type parameter(s). The input string is
by default read from the standard input, but variants exist that read the input from other
sources. The term "scanf" is due to the C language,
which popularized this type of function, but these functions predate C, and other names
are used, such as "readf" in ALGOL 68. Scanf format strings, which provide formatted input,
are complementary to printf format strings, which provide formatted output. In both cases
these provide simple functionality and fixed format compared to more sophisticated and
flexible parsers or template engines, but are sufficient for many purposes.
Usage The scanf function, which is found in C, reads
input for numbers and other datatypes from standard input.
The following shows code in C that reads a variable number of unformatted decimal integers
from the console and prints out each of them on a separate line:
After being processed by the program above, a messy list of integers such as
456 123 789 456 12 456 1
2378
will appear neatly as:
456 123
789 456
12 456
1 2378
To print out a word:
No matter what the datatype the programmer wants the program to read, the arguments 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 is not used for the argument: as word is the
name of an array of char, as such it is equivalent to a pointer to the first element of the array.
While the expression &word 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 scanf output
to strings. As scanf is designated to read only from standard
input, many programming languages with interfaces, such as PHP, have derivatives such as sscanf
and fscanf but not scanf itself. Format string specifications
The formatting placeholders in scanf are more or less the same as that in printf, its reverse
function. There are rarely constants in a format string,
mainly because a program is usually not designed to read known data. The exception is one or
more whitespace characters, which discards all whitespace characters in the input.
Some of the most commonly used placeholders follow:
%d : Scan an integer as a signed decimal number.
%i : Scan an integer as a signed number. Similar to %d, but interprets the number as
hexadecimal when preceded by 0x and octal when preceded by 0. For example, the string
031 would be read as 31 using %d, and 25 using %i. The flag h in %hi indicates conversion
to a short and hh conversion to a char. %u : Scan for decimal unsigned int.) Correspondingly,
%hu scans for an unsigned short and %hhu for an unsigned char.
%f : Scan a floating-point number in normal notation.
%g, %G : Scan a floating-point number in either normal or exponential notation. %g
uses lower-case letters and %G uses upper-case. %x, %X : Scan an integer as an unsigned hexadecimal
number. %o : Scan an integer as an octal number.
%s : Scan a character string. The scan terminates at whitespace. A null character is stored
at the end of the string, which means that the buffer supplied must be at least one character
longer than the specified input length. %c : Scan a character. No null character
is added. whitespace: Any whitespace characters trigger
a scan for zero or more whitespace characters. The number and type of whitespace characters
do not need to match in either direction. %lf : Scan as a double floating-point number.
%Lf : Scan as a long double floating-point number.
The above can be used in compound with numeric modifiers and the l, L modifiers which stand
for "long" in between the percent symbol and the letter. There can also be numeric values
between the percent symbol and the letters, preceding the long modifiers if any, that
specifies the number of characters to be scanned. An optional asterisk 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 ff modifier in printf is not present in scanf, causing differences between modes of
input and output. The ll and hh modifiers are not present in the C90 standard, but are
present in the C99 standard. An example of a format string is
"%7d%s %c%lf" The above format string scans the first seven
characters as a decimal integer, then reads the remaining as a string until a space, new
line or tab is found, then scans the first non-whitespace character following and a double-precision
floating-point number afterwards. Error handling
scanf is usually used in situations when the program cannot guarantee that the input is
in the expected format. Therefore a robust program must check whether the scanf 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 be read and discarded before
new input can be read. An alternative method of reading input, which avoids this, is to
use fgets and then examine the string read in. The last step can be done by sscanf, for
example. Security
Like printf, scanf 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; it can not be determined before
the scanf function is executed. This means that uses of %s 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
can not 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, contain undesirable or even insecure pointers depending on the particular
implementation of varargs. See also
Printf format string C programming language
C++ PHP
Format string attack References
External links scanf(3): input format conversion – Linux
Library Functions Manual C++ reference for std::scanf