scanf. The contents of the format string may contain:
- white space
- This causes the input stream to be read up to the next non-white-space character.
- ordinary character
- Anything except white-space or
%characters. The next character in the input stream must match this character.
- conversion specification
- This is a
%character, followed by an optional
*character (which suppresses the conversion), followed by an optional nonzero decimal integer specifying the maximum field width, an optional
Lto control the length of the conversion and finally a non-optional conversion specifier. Note that use of
Lwill affect the type of pointer which must be used.
[, a field of input is a sequence of non-space characters starting at the first non-space character in the input. It terminates at the first conflicting character or when the input field width is reached.
The result is put into wherever the corresponding argument points, unless the assignment is suppressed using the
*mentioned already. The following conversion specifiers may be used:
d i o u x
- Convert a signed integer, a signed integer in a form acceptable to
strtol, an octal integer, an unsigned integer and a hexadecimal integer respectively.
e f g
- Convert a
float(not a double).
- Read a string, and add a null at the end. The string is terminated by whitespace on input (which is not read as part of the string).
- Read a string. A list of characters, called the scan set follows the
]delimits the list. Characters are read until (but not including) the first character which is not in the scan set. If the first character in the list is a circumflex
^, then the scan set includes any character not in the list. If the initial sequence is
]is not a delimiter, but part of the list and another
]will be needed to end the list. If there is a minus sign (
-) in the list, it must be either the first or the last character; otherwise the meaning is implementation defined.
- Read a single character; white space is significant here. To read the first non-white space character, use
%1s. A field width indicates that an array of characters is to be read.
- Read a (
void *) pointer previously written out using the
%pof one of the
%is expected in the input, no assignment is made.
- Return as an integer the number of characters read by this call so far.
The functions are described below, with the following declarations:
Table 9.7. Size specifiers
| || ||long int|
| || ||short int|
| || ||double|
| || ||long double|
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *s, const char *format, ...);
int scanf(const char *format, ...);
Fscanftakes its input from the designated stream,
scanfis identical to
fscanfwith a first argument of
sscanftakes its input from the designated character array.
If an input failure occurs before any conversion, EOF is returned. Otherwise, the number of successful conversions is returned: this may be zero if no conversions are performed.
An input failure is caused by reading
EOFor reaching the end of the input string (as appropriate). A conversion failure is caused by a failure to match the proper pattern for a particular conversion.