<s>
A	O
scanf	B-Language
format	I-Language
string	I-Language
(	O
scan	O
formatted	O
)	O
is	O
a	O
control	O
parameter	O
used	O
in	O
various	O
functions	O
to	O
specify	O
the	O
layout	O
of	O
an	O
input	O
string	O
.	O
</s>
<s>
String	O
scanning	O
functions	O
are	O
often	O
supplied	O
in	O
standard	O
libraries.Scanf	O
is	O
a	O
function	O
that	O
reads	O
formatted	O
data	O
from	O
the	O
standard	O
input	O
string	O
,	O
which	O
is	O
usually	O
the	O
keyboard	O
and	O
writes	O
the	O
results	O
whenever	O
called	O
in	O
the	O
specified	O
arguments	O
.	O
</s>
<s>
The	O
term	O
"	O
scanf	B-Language
"	O
comes	O
from	O
the	O
C	B-Language
library	I-Language
,	O
which	O
popularized	O
this	O
type	O
of	O
function	O
,	O
but	O
such	O
functions	O
predate	O
C	B-Language
,	O
and	O
other	O
names	O
are	O
used	O
,	O
such	O
as	O
readf	O
in	O
ALGOL	B-Language
68	I-Language
.	O
scanf	B-Language
format	I-Language
strings	I-Language
,	O
which	O
provide	O
formatted	O
input	O
(	O
parsing	B-Language
)	O
,	O
are	O
complementary	O
to	O
printf	B-Language
format	I-Language
strings	I-Language
,	O
which	O
provide	O
formatted	O
output	O
(	O
templating	B-Language
)	O
.	O
</s>
<s>
These	O
provide	O
simple	O
functionality	O
and	O
fixed	O
format	O
compared	O
to	O
more	O
sophisticated	O
and	O
flexible	O
parsers	B-Language
or	O
template	B-Language
engines	O
,	O
but	O
are	O
sufficient	O
for	O
many	O
purposes	O
.	O
</s>
<s>
Mike	O
Lesk	O
's	O
portable	B-Language
input/output	I-Language
library	I-Language
,	O
including	O
scanf	B-Language
,	O
officially	O
became	O
part	O
of	O
Unix	O
in	O
Version	B-Operating_System
7	I-Operating_System
.	O
</s>
<s>
The	O
scanf	B-Language
function	O
,	O
which	O
is	O
found	O
in	O
C	B-Language
,	O
reads	O
input	O
for	O
numbers	O
and	O
other	O
datatypes	O
from	O
standard	O
input	O
(	O
often	O
a	O
command	B-Application
line	I-Application
interface	I-Application
or	O
similar	O
kind	O
of	O
a	O
text	B-Application
user	I-Application
interface	I-Application
)	O
.	O
</s>
<s>
The	O
following	O
C	B-Language
code	O
reads	O
a	O
variable	O
number	O
of	O
unformatted	O
decimal	B-Application
integers	O
from	O
the	O
standard	O
input	O
stream	O
and	O
prints	O
each	O
of	O
them	O
out	O
on	O
separate	O
lines	O
:	O
</s>
<s>
In	O
the	O
last	O
example	O
an	O
address-of	O
operator	O
( &	O
)	O
is	O
not	O
used	O
for	O
the	O
argument	O
:	O
as	O
word	O
is	O
the	O
name	O
of	O
an	O
array	B-Data_Structure
of	O
char	O
,	O
as	O
such	O
it	O
is	O
(	O
in	O
all	O
contexts	O
in	O
which	O
it	O
evaluates	O
to	O
an	O
address	O
)	O
equivalent	O
to	O
a	O
pointer	O
to	O
the	O
first	O
element	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
While	O
the	O
expression	O
&	O
word	O
would	O
numerically	O
evaluate	O
to	O
the	O
same	O
value	O
,	O
semantically	O
,	O
it	O
has	O
an	O
entirely	O
different	O
meaning	O
in	O
that	O
it	O
stands	O
for	O
the	O
address	O
of	O
the	O
whole	O
array	B-Data_Structure
rather	O
than	O
an	O
element	O
of	O
it	O
.	O
</s>
<s>
This	O
fact	O
needs	O
to	O
be	O
kept	O
in	O
mind	O
when	O
assigning	O
scanf	B-Language
output	O
to	O
strings	O
.	O
</s>
<s>
As	O
scanf	B-Language
is	O
designated	O
to	O
read	O
only	O
from	O
standard	O
input	O
,	O
many	O
programming	O
languages	O
with	O
interfaces	B-Application
,	O
such	O
as	O
PHP	B-Application
,	O
have	O
derivatives	O
such	O
as	O
sscanf	O
and	O
fscanf	O
but	O
not	O
scanf	B-Language
itself	O
.	O
</s>
<s>
The	O
formatting	O
placeholders	B-Language
in	O
scanf	B-Language
are	O
more	O
or	O
less	O
the	O
same	O
as	O
that	O
in	O
printf	B-Language
,	O
its	O
reverse	O
function	O
.	O
</s>
<s>
As	O
in	O
printf	B-Language
,	O
the	O
POSIX	O
extension	O
is	O
defined	O
.	O
</s>
<s>
There	O
are	O
rarely	O
constants	O
(	O
i.e.	O
,	O
characters	O
that	O
are	O
not	O
formatting	O
placeholders	B-Language
)	O
in	O
a	O
format	B-Language
string	I-Language
,	O
mainly	O
because	O
a	O
program	O
is	O
usually	O
not	O
designed	O
to	O
read	O
known	O
data	O
,	O
although	O
scanf	B-Language
does	O
accept	O
these	O
if	O
explicitly	O
specified	O
.	O
</s>
<s>
Some	O
of	O
the	O
most	O
commonly	O
used	O
placeholders	B-Language
follow	O
:	O
</s>
<s>
%a	O
:	O
Scan	O
a	O
floating-point	B-Algorithm
number	I-Algorithm
in	O
its	O
hexadecimal	O
notation	O
.	O
</s>
<s>
%d	B-Language
:	O
Scan	O
an	O
integer	O
as	O
a	O
signed	O
decimal	B-Application
number	O
.	O
</s>
<s>
Similar	O
to	O
%d	B-Language
,	O
but	O
interprets	O
the	O
number	O
as	O
hexadecimal	O
when	O
preceded	O
by	O
0x	O
and	O
octal	O
when	O
preceded	O
by	O
0	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
string	O
031	O
would	O
be	O
read	O
as	O
31	O
using	O
%d	B-Language
,	O
and	O
25	O
using	O
%i	O
.	O
</s>
<s>
%u	O
:	O
Scan	O
for	O
decimal	B-Application
unsigned	O
int	O
(	O
Note	O
that	O
in	O
the	O
C99	O
standard	O
the	O
input	O
value	O
minus	O
sign	O
is	O
optional	O
,	O
so	O
if	O
a	O
minus	O
sign	O
is	O
read	O
,	O
no	O
errors	O
will	O
arise	O
and	O
the	O
result	O
will	O
be	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
a	O
negative	O
number	O
,	O
likely	O
a	O
very	O
large	O
value	O
.	O
</s>
<s>
%f	O
:	O
Scan	O
a	O
floating-point	B-Algorithm
number	I-Algorithm
in	O
normal	O
(	O
fixed-point	O
)	O
notation	O
.	O
</s>
<s>
%g	O
,	O
%G	O
:	O
Scan	O
a	O
floating-point	B-Algorithm
number	I-Algorithm
in	O
either	O
normal	O
or	O
exponential	O
notation	O
.	O
</s>
<s>
%s	B-Language
:	O
Scan	O
a	O
character	O
string	O
.	O
</s>
<s>
%c	O
:	O
Scan	O
a	O
character	O
(	O
char	O
)	O
.	O
</s>
<s>
%lf	O
:	O
Scan	O
as	O
a	O
double	O
floating-point	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
"	O
Float	B-Algorithm
"	O
format	O
with	O
the	O
"	O
long	O
"	O
specifier	O
.	O
</s>
<s>
%Lf	O
:	O
Scan	O
as	O
a	O
long	B-Language
double	I-Language
floating-point	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
"	O
Float	B-Algorithm
"	O
format	O
the	O
"	O
long	O
long	O
"	O
specifier	O
.	O
</s>
<s>
The	O
above	O
can	O
be	O
used	O
in	O
compound	O
with	O
numeric	B-Algorithm
modifiers	O
and	O
the	O
l	O
,	O
L	O
modifiers	O
which	O
stand	O
for	O
"	O
long	O
"	O
and	O
"	O
long	O
long	O
"	O
in	O
between	O
the	O
percent	O
symbol	O
and	O
the	O
letter	O
.	O
</s>
<s>
There	O
can	O
also	O
be	O
numeric	B-Algorithm
values	O
between	O
the	O
percent	O
symbol	O
and	O
the	O
letters	O
,	O
preceding	O
the	O
long	O
modifiers	O
if	O
any	O
,	O
that	O
specifies	O
the	O
number	O
of	O
characters	O
to	O
be	O
scanned	O
.	O
</s>
<s>
An	O
optional	O
asterisk	B-Language
( *	O
)	O
right	O
after	O
the	O
percent	O
symbol	O
denotes	O
that	O
the	O
datum	O
read	O
by	O
this	O
format	O
specifier	O
is	O
not	O
to	O
be	O
stored	O
in	O
a	O
variable	O
.	O
</s>
<s>
No	O
argument	O
behind	O
the	O
format	B-Language
string	I-Language
should	O
be	O
included	O
for	O
this	O
dropped	O
variable	O
.	O
</s>
<s>
The	O
ff	O
modifier	O
in	O
printf	B-Language
is	O
not	O
present	O
in	O
scanf	B-Language
,	O
causing	O
differences	O
between	O
modes	O
of	O
input	O
and	O
output	O
.	O
</s>
<s>
The	O
above	O
format	B-Language
string	I-Language
scans	O
the	O
first	O
seven	O
characters	O
as	O
a	O
decimal	B-Application
integer	O
,	O
then	O
reads	O
the	O
remaining	O
as	O
a	O
string	O
until	O
a	O
space	O
,	O
newline	O
,	O
or	O
tab	O
is	O
found	O
,	O
then	O
consumes	O
whitespace	O
until	O
the	O
first	O
non-whitespace	O
character	O
is	O
found	O
,	O
then	O
consumes	O
that	O
character	O
,	O
and	O
finally	O
scans	O
the	O
remaining	O
characters	O
as	O
a	O
double	O
.	O
</s>
<s>
Therefore	O
,	O
a	O
robust	O
program	O
must	O
check	O
whether	O
the	O
scanf	B-Language
call	O
succeeded	O
and	O
take	O
appropriate	O
action	O
.	O
</s>
<s>
In	O
the	O
case	O
of	O
the	O
many	O
float	B-Algorithm
type	O
characters	O
,	O
many	O
implementations	O
choose	O
to	O
collapse	O
most	O
into	O
the	O
same	O
parser	O
.	O
</s>
<s>
Microsoft	O
MSVCRT	O
does	O
it	O
with	O
,	O
while	O
glibc	B-Language
does	O
so	O
with	O
all	O
four	O
.	O
</s>
<s>
scanf	B-Language
is	O
vulnerable	O
to	O
format	B-Language
string	I-Language
attacks	O
.	O
</s>
<s>
Great	O
care	O
should	O
be	O
taken	O
to	O
ensure	O
that	O
the	O
formatting	O
string	O
includes	O
limitations	O
for	O
string	O
and	O
array	B-Data_Structure
sizes	O
.	O
</s>
<s>
In	O
most	O
cases	O
the	O
input	O
string	O
size	O
from	O
a	O
user	O
is	O
arbitrary	O
and	O
cannot	O
be	O
determined	O
before	O
the	O
scanf	B-Language
function	O
is	O
executed	O
.	O
</s>
<s>
This	O
means	O
that	O
%s	B-Language
placeholders	B-Language
without	O
length	O
specifiers	O
are	O
inherently	O
insecure	O
and	O
exploitable	O
for	O
buffer	B-General_Concept
overflows	I-General_Concept
.	O
</s>
<s>
Related	O
to	O
this	O
are	O
additional	O
or	O
mismatched	O
formatting	O
placeholders	B-Language
which	O
do	O
not	O
match	O
the	O
actual	O
vararg	O
list	O
.	O
</s>
<s>
These	O
placeholders	B-Language
might	O
be	O
partially	O
extracted	O
from	O
the	O
stack	O
or	O
contain	O
undesirable	O
or	O
even	O
insecure	O
pointers	O
,	O
depending	O
on	O
the	O
particular	O
implementation	O
of	O
varargs	O
.	O
</s>
