Home

sscanf

sscanf is a function in the C standard library that reads formatted input from a string. It is declared in stdio.h and serves as the string-input counterpart to scanf and fscanf. The function parses the input according to a format string and stores the results in variables supplied through additional arguments.

Signature and behavior: int sscanf(const char *str, const char *format, ...); It reads from str, applying the

Format and conversions: The format string controls how input is consumed. Whitespace in the format matches

Examples and cautions: Example: const char *s = "42 hello"; int n; char w[6]; sscanf(s, "%d %5s", &n,

format
string
to
guide
conversions.
The
additional
arguments
must
be
pointers
to
suitably
typed
variables.
The
function
returns
the
number
of
input
items
successfully
matched
and
assigned;
if
input
fails
before
any
conversion,
it
returns
EOF.
any
amount
of
whitespace
in
the
input.
Literal
characters
in
the
format
must
match
exactly
in
the
string.
Conversion
specifiers
follow
the
scanf
family,
such
as
%d,
%f,
%s,
%c,
%x,
and
%[...].
Field
width
modifiers
(for
example
%10s)
limit
how
many
characters
are
read,
and
length
modifiers
(h,
l,
ll,
L)
adjust
the
target
type.
A
asterisk
after
the
%
(e.g.,
%*d)
suppresses
assignment
for
that
field.
The
%[...]-conversion
reads
a
non-empty
sequence
of
characters
from
a
set
or
its
negation.
w);
This
yields
n
=
42
and
w
=
"hello".
To
avoid
buffer
overflow,
always
provide
field
width
limits
for
string
inputs
and
verify
the
return
value
to
determine
how
many
items
were
parsed.
As
with
other
input
functions,
correctness
depends
on
the
provided
format
and
input
validity.