文章目录
- 1. 格式化输出
-
- 1.1 printf
- 1.2 fprintf
- 1.3 sprintf
- 1.4 snpirntf
- 1.5 函数手册
- 2. 格式化输入
-
- 2.1 scanf
- 2.2 fscanf
- 2.3 sscanf
1. 格式化输出
1.1 printf
int printf(const char *format, ...);
格式化字符串输出到 标准输出流
1.2 fprintf
int fprintf(FILE *stream, const char *format, ...);
格式化字符串输出到 文件流
1.3 sprintf
int sprintf(char *str, const char *format, ...);
格式化字符串 存放到内存缓冲
1.4 snpirntf
int snprintf(char *str, size_t size, const char *format, ...);
格式化字符串 存放到内存缓冲(带有边界检查)
1.5 函数手册
PRINTF(3) Linux Programmer's Manual PRINTF(3)
NAME
printf, fprintf, dprintf, sprintf, snprintf, vprintf, vfprintf,
vdprintf, vsprintf, vsnprintf - formatted output conversion
SYNOPSIS
#include
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
#include
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
snprintf(), vsnprintf():
_BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE ||
_POSIX_C_SOURCE >= 200112L;
or cc -std=c99
dprintf(), vdprintf():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
DESCRIPTION
The functions in the printf() family produce output according to a for‐
mat as described below. The functions printf() and vprintf() write
output to stdout, the standard output stream; fprintf() and vfprintf()
write output to the given output stream; sprintf(), snprintf(),
vsprintf() and vsnprintf() write to the character string str.
The function dprintf() is the same as fprintf(3) except that it outputs
to a file descriptor, fd, instead of to a stdio stream.
The functions snprintf() and vsnprintf() write at most size bytes
(including the terminating null byte ('\0')) to str.
The functions vprintf(), vfprintf(), vdprintf(), vsprintf(),
vsnprintf() are equivalent to the functions printf(), fprintf(),
dprintf(), sprintf(), snprintf(), respectively, except that they are
called with a va_list instead of a variable number of arguments. These
functions do not call the va_end macro. Because they invoke the va_arg
macro, the value of ap is undefined after the call. See stdarg(3).
All of these functions write the output under the control of a format
string that specifies how subsequent arguments (or arguments accessed
via the variable-length argument facilities of stdarg(3)) are converted
for output.
C99 and POSIX.1-2001 specify that the results are undefined if a call
to sprintf(), snprintf(), vsprintf(), or vsnprintf() would cause copy‐
ing to take place between objects that overlap (e.g., if the target
string array and one of the supplied input arguments refer to the same
buffer). See NOTES.
Format of the format string
The format string is a character string, beginning and ending in its
initial shift state, if any. The format string is composed of zero or
more directives: ordinary characters (not %), which are copied
unchanged to the output stream; and conversion specifications, each of
which results in fetching zero or more subsequent arguments. Each con‐
version specification is introduced by the character %, and ends with a
conversion specifier. In between there may be (in this order) zero or
more flags, an optional minimum field width, an optional precision and
an optional length modifier.
The arguments must correspond properly (after type promotion) with the
conversion specifier. By default, the arguments are used in the order
given, where each '*' (see Field width and Precision below) and each
conversion specifier asks for the next argument (and it is an error if
insufficiently many arguments are given). One can also specify explic‐
itly which argument is taken, at each place where an argument is
required, by writing "%m$" instead of '%' and "*m$" instead of '*',
where the decimal integer m denotes the position in the argument list
of the desired argument, indexed starting from 1. Thus,
printf("%*d", width, num);
and
printf("%2$*1$d", width, num);
are equivalent. The second style allows repeated references to the
same argument. The C99 standard does not include the style using '$',
which comes from the Single UNIX Specification. If the style using '$'
is used, it must be used throughout for all conversions taking an argu‐
ment and all width and precision arguments, but it may be mixed with
"%%" formats, which do not consume an argument. There may be no gaps
in the numbers of arguments specified using '$'; for example, if argu‐
ments 1 and 3 are specified, argument 2 must also be specified some‐
where in the format string.
For some numeric conversions a radix character ("decimal point") or
thousands' grouping character is used. The actual character used
depends on the LC_NUMERIC part of the locale. The POSIX locale uses
'.' as radix character, and does not have a grouping character. Thus,
printf("%'.2f", 1234567.89);
results in "1234567.89" in the POSIX locale, in "1234567,89" in the
nl_NL locale, and in "1.234.567,89" in the da_DK locale.
Flag characters
The character % is followed by zero or more of the following flags:
# The value should be converted to an "alternate form". For o
conversions, the first character of the output string is made
zero (by prefixing a 0 if it was not zero already). For x and X
conversions, a nonzero result has the string "0x" (or "0X" for X
conversions) prepended to it. For a, A, e, E, f, F, g, and G
conversions, the result will always contain a decimal point,
even if no digits follow it (normally, a decimal point appears
in the results of those conversions only if a digit follows).
For g and G conversions, trailing zeros are not removed from the
result as they would otherwise be. For other conversions, the
result is undefined.
0 The value should be zero padded. For d, i, o, u, x, X, a, A, e,
E, f, F, g, and G conversions, the converted value is padded on
the left with zeros rather than blanks. If the 0 and - flags
both appear, the 0 flag is ignored. If a precision is given
with a numeric conversion (d, i, o, u, x, and X), the 0 flag is
ignored. For other conversions, the behavior is undefined.
- The converted value is to be left adjusted on the field bound‐
ary. (The default is right justification.) The converted value
is padded on the right with blanks, rather than on the left with
blanks or zeros. A - overrides a 0 if both are given.
' ' (a space) A blank should be left before a positive number (or
empty string) produced by a signed conversion.
+ A sign (+ or -) should always be placed before a number produced
by a signed conversion. By default, a sign is used only for
negative numbers. A + overrides a space if both are used.
The five flag characters above are defined in the C99 standard. The
Single UNIX Specification specifies one further flag character.
' For decimal conversion (i, d, u, f, F, g, G) the output is to be
grouped with thousands' grouping characters if the locale infor‐
mation indicates any. Note that many versions of gcc(1) cannot
parse this option and will issue a warning. (SUSv2 did not
include %'F, but SUSv3 added it.)
glibc 2.2 adds one further flag character.
I For decimal integer conversion (i, d, u) the output uses the
locale's alternative output digits, if any. For example, since
glibc 2.2.3 this will give Arabic-Indic digits in the Persian
("fa_IR") locale.
Field width
An optional decimal digit string (with nonzero first digit) specifying
a minimum field width. If the converted value has fewer characters
than the field width, it will be padded with spaces on the left (or
right, if the left-adjustment flag has been given). Instead of a deci‐
mal digit string one may write "*" or "*m$" (for some decimal integer
m) to specify that the field width is given in the next argument, or in
the m-th argument, respectively, which must be of type int. A negative
field width is taken as a '-' flag followed by a positive field width.
In no case does a nonexistent or small field width cause truncation of
a field; if the result of a conversion is wider than the field width,
the field is expanded to contain the conversion result.
Precision
An optional precision, in the form of a period ('.') followed by an
optional decimal digit string. Instead of a decimal digit string one
may write "*" or "*m$" (for some decimal integer m) to specify that the
precision is given in the next argument, or in the m-th argument,
respectively, which must be of type int. If the precision is given as
just '.', the precision is taken to be zero. A negative precision is
taken as if the precision were omitted. This gives the minimum number
of digits to appear for d, i, o, u, x, and X conversions, the number of
digits to appear after the radix character for a, A, e, E, f, and F
conversions, the maximum number of significant digits for g and G con‐
versions, or the maximum number of characters to be printed from a
string for s and S conversions.
Length modifier
Here, "integer conversion" stands for d, i, o, u, x, or X conversion.
hh A following integer conversion corresponds to a signed char or
unsigned char argument, or a following n conversion corresponds
to a pointer to a signed char argument.
h A following integer conversion corresponds to a short int or
unsigned short int argument, or a following n conversion corre‐
sponds to a pointer to a short int argument.
l (ell) A following integer conversion corresponds to a long int
or unsigned long int argument, or a following n conversion cor‐
responds to a pointer to a long int argument, or a following c
conversion corresponds to a wint_t argument, or a following s
conversion corresponds to a pointer to wchar_t argument.
ll (ell-ell). A following integer conversion corresponds to a long
long int or unsigned long long int argument, or a following n
conversion corresponds to a pointer to a long long int argument.
L A following a, A, e, E, f, F, g, or G conversion corresponds to
a long double argument. (C99 allows %LF, but SUSv2 does not.)
This is a synonym for ll.
j A following integer conversion corresponds to an intmax_t or
uintmax_t argument, or a following n conversion corresponds to a
pointer to an intmax_t argument.
z A following integer conversion corresponds to a size_t or
ssize_t argument, or a following n conversion corresponds to a
pointer to a size_t argument.
t A following integer conversion corresponds to a ptrdiff_t argu‐
ment, or a following n conversion corresponds to a pointer to a
ptrdiff_t argument.
SUSv3 specifies all of the above. SUSv2 specified only the length mod‐
ifiers h (in hd, hi, ho, hx, hX, hn) and l (in ld, li, lo, lx, lX, ln,
lc, ls) and L (in Le, LE, Lf, Lg, LG).
Conversion specifiers
A character that specifies the type of conversion to be applied. The
conversion specifiers and their meanings are:
d, i The int argument is converted to signed decimal notation. The
precision, if any, gives the minimum number of digits that must
appear; if the converted value requires fewer digits, it is
padded on the left with zeros. The default precision is 1.
When 0 is printed with an explicit precision 0, the output is
empty.
o, u, x, X
The unsigned int argument is converted to unsigned octal (o),
unsigned decimal (u), or unsigned hexadecimal (x and X) nota‐
tion. The letters abcdef are used for x conversions; the let‐
ters ABCDEF are used for X conversions. The precision, if any,
gives the minimum number of digits that must appear; if the con‐
verted value requires fewer digits, it is padded on the left
with zeros. The default precision is 1. When 0 is printed with
an explicit precision 0, the output is empty.
e, E The double argument is rounded and converted in the style
[-]d.ddde±dd where there is one digit before the decimal-point
character and the number of digits after it is equal to the pre‐
cision; if the precision is missing, it is taken as 6; if the
precision is zero, no decimal-point character appears. An E
conversion uses the letter E (rather than e) to introduce the
exponent. The exponent always contains at least two digits; if
the value is zero, the exponent is 00.
f, F The double argument is rounded and converted to decimal notation
in the style [-]ddd.ddd, where the number of digits after the
decimal-point character is equal to the precision specification.
If the precision is missing, it is taken as 6; if the precision
is explicitly zero, no decimal-point character appears. If a
decimal point appears, at least one digit appears before it.
(SUSv2 does not know about F and says that character string rep‐
resentations for infinity and NaN may be made available. SUSv3
adds a specification for F. The C99 standard specifies "[-]inf"
or "[-]infinity" for infinity, and a string starting with "nan"
for NaN, in the case of f conversion, and "[-]INF" or "[-]INFIN‐
ITY" or "NAN*" in the case of F conversion.)
g, G The double argument is converted in style f or e (or F or E for
G conversions). The precision specifies the number of signifi‐
cant digits. If the precision is missing, 6 digits are given;
if the precision is zero, it is treated as 1. Style e is used
if the exponent from its conversion is less than -4 or greater
than or equal to the precision. Trailing zeros are removed from
the fractional part of the result; a decimal point appears only
if it is followed by at least one digit.
a, A (C99; not in SUSv2, but added in SUSv3) For a conversion, the
double argument is converted to hexadecimal notation (using the
letters abcdef) in the style [-]0xh.hhhhp±; for A conversion the
prefix 0X, the letters ABCDEF, and the exponent separator P is
used. There is one hexadecimal digit before the decimal point,
and the number of digits after it is equal to the precision.
The default precision suffices for an exact representation of
the value if an exact representation in base 2 exists and other‐
wise is sufficiently large to distinguish values of type double.
The digit before the decimal point is unspecified for nonnormal‐
ized numbers, and nonzero but otherwise unspecified for normal‐
ized numbers.
c If no l modifier is present, the int argument is converted to an
unsigned char, and the resulting character is written. If an l
modifier is present, the wint_t (wide character) argument is
converted to a multibyte sequence by a call to the wcrtomb(3)
function, with a conversion state starting in the initial state,
and the resulting multibyte string is written.
s If no l modifier is present: The const char * argument is
expected to be a pointer to an array of character type (pointer
to a string). Characters from the array are written up to (but
not including) a terminating null byte ('\0'); if a precision is
specified, no more than the number specified are written. If a
precision is given, no null byte need be present; if the preci‐
sion is not specified, or is greater than the size of the array,
the array must contain a terminating null byte.
If an l modifier is present: The const wchar_t * argument is
expected to be a pointer to an array of wide characters. Wide
characters from the array are converted to multibyte characters
(each by a call to the wcrtomb(3) function, with a conversion
state starting in the initial state before the first wide char‐
acter), up to and including a terminating null wide character.
The resulting multibyte characters are written up to (but not
including) the terminating null byte. If a precision is speci‐
fied, no more bytes than the number specified are written, but
no partial multibyte characters are written. Note that the pre‐
cision determines the number of bytes written, not the number of
wide characters or screen positions. The array must contain a
terminating null wide character, unless a precision is given and
it is so small that the number of bytes written exceeds it
before the end of the array is reached.
C (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym
for lc. Don't use.
S (Not in C99 or C11, but in SUSv2, SUSv3, and SUSv4.) Synonym
for ls. Don't use.
p The void * pointer argument is printed in hexadecimal (as if by
%#x or %#lx).
n The number of characters written so far is stored into the inte‐
ger pointed to by the corresponding argument. That argument
shall be an int *, or variant whose size matches the (option‐
ally) supplied integer length modifier. No argument is con‐
verted. The behavior is undefined if the conversion specifica‐
tion includes any flags, a field width, or a precision.
m (Glibc extension.) Print output of strerror(errno). No argu‐
ment is required.
% A '%' is written. No argument is converted. The complete con‐
version specification is '%%'.
RETURN VALUE
Upon successful return, these functions return the number of characters
printed (excluding the null byte used to end output to strings).
The functions snprintf() and vsnprintf() do not write more than size
bytes (including the terminating null byte ('\0')). If the output was
truncated due to this limit, then the return value is the number of
characters (excluding the terminating null byte) which would have been
written to the final string if enough space had been available. Thus,
a return value of size or more means that the output was truncated.
(See also below under NOTES.)
If an output error is encountered, a negative value is returned.
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
┌────────────────────────┬───────────────┬────────────────┐
│Interface │ Attribute │ Value │
├────────────────────────┼───────────────┼────────────────┤
│printf(), fprintf(), │ Thread safety │ MT-Safe locale │
│sprintf(), snprintf(), │ │ │
│vprintf(), vfprintf(), │ │ │
│vsprintf(), vsnprintf() │ │ │
└────────────────────────┴───────────────┴────────────────┘
CONFORMING TO
fprintf(), printf(), sprintf(), vprintf(), vfprintf(), vsprintf():
POSIX.1-2001, POSIX.1-2008, C89, C99.
snprintf(), vsnprintf(): POSIX.1-2001, POSIX.1-2008, C99.
The dprintf() and vdprintf() functions were originally GNU extensions
that were later standardized in POSIX.1-2008.
Concerning the return value of snprintf(), SUSv2 and C99 contradict
each other: when snprintf() is called with size=0 then SUSv2 stipulates
an unspecified return value less than 1, while C99 allows str to be
NULL in this case, and gives the return value (as always) as the number
of characters that would have been written in case the output string
has been large enough. POSIX.1-2001 and later align their specifica‐
tion of snprintf() with C99.
glibc 2.1 adds length modifiers hh, j, t, and z and conversion charac‐
ters a and A.
glibc 2.2 adds the conversion character F with C99 semantics, and the
flag character I.
2. 格式化输入
2.1 scanf
int scanf(const char *format, ...);
标准输入流获取数据
2.2 fscanf
int fscanf(FILE *stream, const char *format, ...);
文件流获取数据
2.3 sscanf
int sscanf(const char *str, const char *format, ...);
从字符串str获取数据
NAME
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf - input format conver‐
sion
SYNOPSIS
#include
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
#include
int vscanf(const char *format, va_list ap);
int vsscanf(const char *str, const char *format, va_list ap);
int vfscanf(FILE *stream, const char *format, va_list ap);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
vscanf(), vsscanf(), vfscanf():
_XOPEN_SOURCE >= 600 || _ISOC99_SOURCE ||
_POSIX_C_SOURCE >= 200112L;
or cc -std=c99
DESCRIPTION
The scanf() family of functions scans input according to format as
described below. This format may contain conversion specifications;
the results from such conversions, if any, are stored in the locations
pointed to by the pointer arguments that follow format. Each pointer
argument must be of a type that is appropriate for the value returned
by the corresponding conversion specification.
If the number of conversion specifications in format exceeds the number
of pointer arguments, the results are undefined. If the number of
pointer arguments exceeds the number of conversion specifications, then
the excess pointer arguments are evaluated, but are otherwise ignored.
The scanf() function reads input from the standard input stream stdin,
fscanf() reads input from the stream pointer stream, and sscanf() reads
its input from the character string pointed to by str.
The vfscanf() function is analogous to vfprintf(3) and reads input from
the stream pointer stream using a variable argument list of pointers
(see stdarg(3). The vscanf() function scans a variable argument list
from the standard input and the vsscanf() function scans it from a
string; these are analogous to the vprintf(3) and vsprintf(3) functions
respectively.
The format string consists of a sequence of directives which describe
how to process the sequence of input characters. If processing of a
directive fails, no further input is read, and scanf() returns. A
"failure" can be either of the following: input failure, meaning that
input characters were unavailable, or matching failure, meaning that
the input was inappropriate (see below).
A directive is one of the following:
· A sequence of white-space characters (space, tab, newline, etc.;
see isspace(3)). This directive matches any amount of white
space, including none, in the input.
· An ordinary character (i.e., one other than white space or '%').
This character must exactly match the next character of input.
· A conversion specification, which commences with a '%' (percent)
character. A sequence of characters from the input is converted
according to this specification, and the result is placed in the
corresponding pointer argument. If the next item of input does
not match the conversion specification, the conversion fails—
this is a matching failure.
Each conversion specification in format begins with either the charac‐
ter '%' or the character sequence "%n$" (see below for the distinction)
followed by:
· An optional '*' assignment-suppression character: scanf() reads
input as directed by the conversion specification, but discards
the input. No corresponding pointer argument is required, and
this specification is not included in the count of successful
assignments returned by scanf().
· An optional 'm' character. This is used with string conversions
(%s, %c, %[), and relieves the caller of the need to allocate a
corresponding buffer to hold the input: instead, scanf() allo‐
cates a buffer of sufficient size, and assigns the address of
this buffer to the corresponding pointer argument, which should
be a pointer to a char * variable (this variable does not need
to be initialized before the call). The caller should subse‐
quently free(3) this buffer when it is no longer required.
· An optional decimal integer which specifies the maximum field
width. Reading of characters stops either when this maximum is
reached or when a nonmatching character is found, whichever hap‐
pens first. Most conversions discard initial white space char‐
acters (the exceptions are noted below), and these discarded
characters don't count toward the maximum field width. String
input conversions store a terminating null byte ('\0') to mark
the end of the input; the maximum field width does not include
this terminator.
· An optional type modifier character. For example, the l type
modifier is used with integer conversions such as %d to specify
that the corresponding pointer argument refers to a long int
rather than a pointer to an int.
· A conversion specifier that specifies the type of input conver‐
sion to be performed.
The conversion specifications in format are of two forms, either begin‐
ning with '%' or beginning with "%n$". The two forms should not be
mixed in the same format string, except that a string containing "%n$"
specifications can include %% and %*. If format contains '%' specifi‐
cations, then these correspond in order with successive pointer argu‐
ments. In the "%n$" form (which is specified in POSIX.1-2001, but not
C99), n is a decimal integer that specifies that the converted input
should be placed in the location referred to by the n-th pointer argu‐
ment following format.
Conversions
The following type modifier characters can appear in a conversion spec‐
ification:
h Indicates that the conversion will be one of d, i, o, u, x, X,
or n and the next pointer is a pointer to a short int or
unsigned short int (rather than int).
hh As for h, but the next pointer is a pointer to a signed char or
unsigned char.
j As for h, but the next pointer is a pointer to an intmax_t or a
uintmax_t. This modifier was introduced in C99.
l Indicates either that the conversion will be one of d, i, o, u,
x, X, or n and the next pointer is a pointer to a long int or
unsigned long int (rather than int), or that the conversion will
be one of e, f, or g and the next pointer is a pointer to double
(rather than float). Specifying two l characters is equivalent
to L. If used with %c or %s, the corresponding parameter is
considered as a pointer to a wide character or wide-character
string respectively.
L Indicates that the conversion will be either e, f, or g and the
next pointer is a pointer to long double or the conversion will
be d, i, o, u, or x and the next pointer is a pointer to long
long.
q equivalent to L. This specifier does not exist in ANSI C.
t As for h, but the next pointer is a pointer to a ptrdiff_t.
This modifier was introduced in C99.
z As for h, but the next pointer is a pointer to a size_t. This
modifier was introduced in C99.
The following conversion specifiers are available:
% Matches a literal '%'. That is, %% in the format string matches
a single input '%' character. No conversion is done (but ini‐
tial white space characters are discarded), and assignment does
not occur.
d Matches an optionally signed decimal integer; the next pointer
must be a pointer to int.
D Equivalent to ld; this exists only for backward compatibility.
(Note: thus only in libc4. In libc5 and glibc the %D is
silently ignored, causing old programs to fail mysteriously.)
i Matches an optionally signed integer; the next pointer must be a
pointer to int. The integer is read in base 16 if it begins
with 0x or 0X, in base 8 if it begins with 0, and in base 10
otherwise. Only characters that correspond to the base are
used.
o Matches an unsigned octal integer; the next pointer must be a
pointer to unsigned int.
u Matches an unsigned decimal integer; the next pointer must be a
pointer to unsigned int.
x Matches an unsigned hexadecimal integer; the next pointer must
be a pointer to unsigned int.
X Equivalent to x.
f Matches an optionally signed floating-point number; the next
pointer must be a pointer to float.
e Equivalent to f.
g Equivalent to f.
E Equivalent to f.
a (C99) Equivalent to f.
s Matches a sequence of non-white-space characters; the next
pointer must be a pointer to the initial element of a character
array that is long enough to hold the input sequence and the
terminating null byte ('\0'), which is added automatically. The
input string stops at white space or at the maximum field width,
whichever occurs first.
c Matches a sequence of characters whose length is specified by
the maximum field width (default 1); the next pointer must be a
pointer to char, and there must be enough room for all the char‐
acters (no terminating null byte is added). The usual skip of
leading white space is suppressed. To skip white space first,
use an explicit space in the format.
[ Matches a nonempty sequence of characters from the specified set
of accepted characters; the next pointer must be a pointer to
char, and there must be enough room for all the characters in
the string, plus a terminating null byte. The usual skip of
leading white space is suppressed. The string is to be made up
of characters in (or not in) a particular set; the set is
defined by the characters between the open bracket [ character
and a close bracket ] character. The set excludes those charac‐
ters if the first character after the open bracket is a circum‐
flex (^). To include a close bracket in the set, make it the
first character after the open bracket or the circumflex; any
other position will end the set. The hyphen character - is also
special; when placed between two other characters, it adds all
intervening characters to the set. To include a hyphen, make it
the last character before the final close bracket. For
instance, [^]0-9-] means the set "everything except close
bracket, zero through nine, and hyphen". The string ends with
the appearance of a character not in the (or, with a circumflex,
in) set or when the field width runs out.
p Matches a pointer value (as printed by %p in printf(3); the next
pointer must be a pointer to a pointer to void.
n Nothing is expected; instead, the number of characters consumed
thus far from the input is stored through the next pointer,
which must be a pointer to int. This is not a conversion and
does not increase the count returned by the function. The
assignment can be suppressed with the * assignment-suppression
character, but the effect on the return value is undefined.
Therefore %*n conversions should not be used.
RETURN VALUE
On success, these functions return the number of input items success‐
fully matched and assigned; this can be fewer than provided for, or
even zero, in the event of an early matching failure.
The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set to indicate the
error.
ERRORS
EAGAIN The file descriptor underlying stream is marked nonblocking, and
the read operation would block.
EBADF The file descriptor underlying stream is invalid, or not open
for reading.
EILSEQ Input byte sequence does not form a valid character.
EINTR The read operation was interrupted by a signal; see signal(7).
EINVAL Not enough arguments; or format is NULL.
ENOMEM Out of memory.
ERANGE The result of an integer conversion would exceed the size that
can be stored in the corresponding integer type.
ATTRIBUTES
For an explanation of the terms used in this section, see
attributes(7).
┌─────────────────────┬───────────────┬────────────────┐
│Interface │ Attribute │ Value │
├─────────────────────┼───────────────┼────────────────┤
│scanf(), fscanf(), │ Thread safety │ MT-Safe locale │
│sscanf(), vscanf(), │ │ │
│vsscanf(), vfscanf() │ │ │
└─────────────────────┴───────────────┴────────────────┘
CONFORMING TO
The functions fscanf(), scanf(), and sscanf() conform to C89 and C99
and POSIX.1-2001. These standards do not specify the ERANGE error.
The q specifier is the 4.4BSD notation for long long, while ll or the
usage of L in integer conversions is the GNU notation.
The Linux version of these functions is based on the GNU libio library.
Take a look at the info documentation of GNU libc (glibc-1.08) for a
more concise description.