<s>
The	O
printf	B-Language
format	I-Language
string	I-Language
is	O
a	O
control	O
parameter	O
used	O
by	O
a	O
class	O
of	O
functions	O
in	O
the	O
input/output	O
libraries	O
of	O
C	B-Language
and	O
many	O
other	O
programming	O
languages	O
.	O
</s>
<s>
The	O
string	O
is	O
written	O
in	O
a	O
simple	O
template	B-Language
language	I-Language
:	O
characters	O
are	O
usually	O
copied	O
literally	O
into	O
the	O
function	O
's	O
output	O
,	O
but	O
format	B-Language
specifiers	I-Language
,	O
which	O
start	O
with	O
a	O
character	O
,	O
indicate	O
the	O
location	O
and	O
method	O
to	O
translate	O
a	O
piece	O
of	O
data	O
(	O
such	O
as	O
a	O
number	O
)	O
to	O
characters	O
.	O
</s>
<s>
"	O
printf	B-Device
"	O
is	O
the	O
name	O
of	O
one	O
of	O
the	O
main	O
C	B-Language
output	O
functions	O
,	O
and	O
stands	O
for	O
"	O
print	O
formatted	O
"	O
.	O
</s>
<s>
printf	B-Language
format	I-Language
strings	I-Language
are	O
complementary	O
to	O
scanf	B-Language
format	I-Language
strings	I-Language
,	O
which	O
provide	O
formatted	O
input	O
(	O
lexing	B-Application
aka	O
.	O
</s>
<s>
parsing	B-Language
)	O
.	O
</s>
<s>
In	O
both	O
cases	O
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
template	B-Language
engines	O
or	O
lexers/parsers	O
,	O
but	O
are	O
sufficient	O
for	O
many	O
purposes	O
.	O
</s>
<s>
Many	O
languages	O
other	O
than	O
C	B-Language
copy	O
the	O
printf	B-Language
format	I-Language
string	I-Language
syntax	O
closely	O
or	O
exactly	O
in	O
their	O
own	O
I/O	O
functions	O
.	O
</s>
<s>
Mismatches	O
between	O
the	O
format	B-Language
specifiers	I-Language
and	O
type	O
of	O
the	O
data	O
can	O
cause	O
crashes	O
and	O
other	O
vulnerabilities	O
.	O
</s>
<s>
The	O
format	B-Language
string	I-Language
itself	O
is	O
very	O
often	O
a	O
string	B-Language
literal	I-Language
,	O
which	O
allows	O
static	O
analysis	O
of	O
the	O
function	O
call	O
.	O
</s>
<s>
However	O
,	O
it	O
can	O
also	O
be	O
the	O
value	O
of	O
a	O
variable	O
,	O
which	O
allows	O
for	O
dynamic	O
formatting	O
but	O
also	O
a	O
security	O
vulnerability	O
known	O
as	O
an	O
uncontrolled	O
format	B-Language
string	I-Language
exploit	O
.	O
</s>
<s>
Early	O
programming	O
languages	O
such	O
as	O
Fortran	B-Application
used	O
special	O
statements	O
with	O
completely	O
different	O
syntax	O
from	O
other	O
calculations	O
to	O
build	O
formatting	O
descriptions	O
.	O
</s>
<s>
ALGOL	B-Language
68	I-Language
had	O
more	O
function-like	O
API	B-Application
,	O
but	O
still	O
used	O
special	O
syntax	O
(	O
the	O
delimiters	O
surround	O
special	O
formatting	O
syntax	O
)	O
:	O
</s>
<s>
C	B-Language
's	O
has	O
its	O
origins	O
in	O
BCPL	B-Language
's	O
function	O
(	O
1966	O
)	O
.	O
</s>
<s>
In	O
comparison	O
to	O
and	O
,	O
is	O
a	O
BCPL	B-Language
language	I-Language
escape	O
sequence	O
representing	O
a	O
newline	O
character	O
(	O
for	O
which	O
C	B-Language
uses	O
the	O
escape	O
sequence	O
)	O
and	O
the	O
order	O
of	O
the	O
format	O
specification	O
's	O
field	O
width	O
and	O
type	O
is	O
reversed	O
in	O
:	O
</s>
<s>
Probably	O
the	O
first	O
copying	O
of	O
the	O
syntax	O
outside	O
the	O
C	B-Language
language	I-Language
was	O
the	O
Unix	O
shell	O
command	O
,	O
which	O
first	O
appeared	O
in	O
Version	B-Operating_System
4	I-Operating_System
,	O
as	O
part	O
of	O
the	O
port	O
to	O
C	B-Language
.	O
</s>
<s>
Formatting	O
takes	O
place	O
via	O
placeholders	B-Language
within	O
the	O
format	B-Language
string	I-Language
.	O
</s>
<s>
For	O
example	O
,	O
if	O
a	O
program	O
wanted	O
to	O
print	O
out	O
a	O
person	O
's	O
age	O
,	O
it	O
could	O
present	O
the	O
output	O
by	O
prefixing	O
it	O
with	O
"	O
Your	O
age	O
is	O
"	O
,	O
and	O
using	O
the	O
signed	O
decimal	O
specifier	O
character	O
to	O
denote	O
that	O
we	O
want	O
the	O
integer	O
for	O
the	O
age	O
to	O
be	O
shown	O
immediately	O
after	O
that	O
message	O
,	O
we	O
may	O
use	O
the	O
format	B-Language
string	I-Language
:	O
</s>
<s>
This	O
is	O
a	O
POSIX	O
extension	O
and	O
not	O
in	O
C99	B-Language
.	O
</s>
<s>
Character	O
Description	O
n	O
is	O
the	O
number	O
of	O
the	O
parameter	O
to	O
display	O
using	O
this	O
format	O
specifier	O
,	O
allowing	O
the	O
parameters	O
provided	O
to	O
be	O
output	O
multiple	O
times	O
,	O
using	O
varying	O
format	B-Language
specifiers	I-Language
or	O
in	O
different	O
orders	O
.	O
</s>
<s>
If	O
any	O
single	O
placeholder	B-Language
specifies	O
a	O
parameter	O
,	O
all	O
the	O
rest	O
of	O
the	O
placeholders	B-Language
MUST	O
also	O
specify	O
a	O
parameter	O
.	O
</s>
<s>
Character	O
Description	O
(	O
minus	O
)	O
Left-align	O
the	O
output	O
of	O
this	O
placeholder	B-Language
.	O
</s>
<s>
ISO/IEC	O
9899:1999(E )	O
:	O
Programming	O
Languages	O
–	O
C	B-Language
§	O
7.19.6.1	O
para	O
7	O
.	O
</s>
<s>
Additionally	O
,	O
several	O
platform-specific	O
length	O
options	O
came	O
to	O
exist	O
prior	O
to	O
widespread	O
use	O
of	O
the	O
ISO	O
C99	B-Language
extensions	O
:	O
</s>
<s>
ISO	O
C99	B-Language
includes	O
the	O
inttypes.h	O
header	O
file	O
that	O
includes	O
a	O
number	O
of	O
macros	O
for	O
use	O
in	O
platform-independent	O
coding	O
.	O
</s>
<s>
and	O
are	O
synonymous	O
for	O
output	O
,	O
but	O
are	O
different	O
when	O
used	O
with	O
scanf	B-Language
for	O
input	O
(	O
where	O
using	O
will	O
interpret	O
a	O
number	O
as	O
hexadecimal	O
if	O
it	O
's	O
preceded	O
by	O
,	O
and	O
octal	O
if	O
it	O
's	O
preceded	O
by	O
.	O
)	O
</s>
<s>
null-terminated	B-Data_Structure
string	I-Data_Structure
.	O
</s>
<s>
(	O
added	O
in	O
C99	B-Language
)	O
(	O
C++11	O
iostreams	O
have	O
a	O
that	O
works	O
the	O
same	O
)	O
.	O
</s>
<s>
Print	O
nothing	O
,	O
but	O
writes	O
the	O
number	O
of	O
characters	O
written	O
so	O
far	O
into	O
an	O
integer	O
pointer	O
parameter.In	O
Java	B-Language
this	O
prints	O
a	O
newline	O
.	O
</s>
<s>
There	O
are	O
a	O
few	O
implementations	O
of	O
-like	O
functions	O
that	O
allow	O
extensions	O
to	O
the	O
escape-character-based	O
mini-language	B-Language
,	O
thus	O
allowing	O
the	O
programmer	O
to	O
have	O
a	O
specific	O
formatting	O
function	O
for	O
non-builtin	O
types	O
.	O
</s>
<s>
One	O
of	O
the	O
most	O
well-known	O
is	O
the	O
(	O
now	O
deprecated	O
)	O
glibc	B-Language
's	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
rarely	O
used	O
due	O
to	O
the	O
fact	O
that	O
it	O
conflicts	O
with	O
static	O
format	B-Language
string	I-Language
checking	O
.	O
</s>
<s>
Some	O
applications	O
(	O
like	O
the	O
Apache	B-Application
HTTP	I-Application
Server	I-Application
)	O
include	O
their	O
own	O
-like	O
function	O
,	O
and	O
embed	O
extensions	O
into	O
it	O
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
printk	B-Application
function	O
supports	O
a	O
number	O
of	O
ways	O
to	O
display	O
kernel	B-Operating_System
structures	O
using	O
the	O
generic	O
specification	O
,	O
by	O
appending	O
additional	O
format	O
characters	O
.	O
</s>
<s>
For	O
example	O
,	O
prints	O
an	O
IPv4	B-Protocol
address	O
in	O
dotted-decimal	O
form	O
.	O
</s>
<s>
This	O
allows	O
static	O
format	B-Language
string	I-Language
checking	O
(	O
of	O
the	O
portion	O
)	O
at	O
the	O
expense	O
of	O
full	O
compatibility	O
with	O
normal	O
printf	B-Device
.	O
</s>
<s>
If	O
there	O
are	O
too	O
few	O
function	O
arguments	O
provided	O
to	O
supply	O
values	O
for	O
all	O
the	O
conversion	O
specifications	O
in	O
the	O
template	B-Language
string	O
,	O
or	O
if	O
the	O
arguments	O
are	O
not	O
of	O
the	O
correct	O
types	O
,	O
the	O
results	O
are	O
undefined	O
,	O
may	O
crash	O
.	O
</s>
<s>
In	O
a	O
number	O
of	O
cases	O
,	O
the	O
undefined	O
behavior	O
has	O
led	O
to	O
"	O
Format	B-Language
string	I-Language
attack	O
"	O
security	O
vulnerabilities	O
.	O
</s>
<s>
In	O
most	O
C	B-Language
or	O
C++	B-Language
calling	O
conventions	O
arguments	O
may	O
be	O
passed	O
on	O
the	O
stack	O
,	O
which	O
means	O
in	O
the	O
case	O
of	O
too	O
few	O
arguments	O
printf	B-Device
will	O
read	O
past	O
the	O
end	O
of	O
the	O
current	O
stackframe	O
,	O
thus	O
allowing	O
the	O
attacker	O
to	O
read	O
the	O
stack	O
.	O
</s>
<s>
Some	O
compilers	O
,	O
like	O
the	B-Application
GNU	I-Application
Compiler	I-Application
Collection	I-Application
,	O
will	O
statically	O
check	O
the	O
format	B-Language
strings	I-Language
of	O
printf-like	O
functions	O
and	O
warn	O
about	O
problems	O
(	O
when	O
using	O
the	O
flags	O
or	O
)	O
.	O
</s>
<s>
GCC	B-Application
will	O
also	O
warn	O
about	O
user-defined	O
printf-style	O
functions	O
if	O
the	O
non-standard	O
"	O
format	O
"	O
is	O
applied	O
to	O
the	O
function	O
.	O
</s>
<s>
A	O
game	O
of	O
tic-tac-toe	O
written	O
in	O
the	O
format	B-Language
string	I-Language
is	O
a	O
winner	O
of	O
the	O
27th	O
IOCCC	O
.	O
</s>
<s>
Not	O
included	O
in	O
this	O
list	O
are	O
languages	O
that	O
use	O
format	B-Language
strings	I-Language
that	O
deviate	O
from	O
the	O
style	O
in	O
this	O
article	O
(	O
such	O
as	O
AMPL	B-Language
and	O
Elixir	B-Language
)	O
,	O
languages	O
that	O
inherit	O
their	O
implementation	O
from	O
the	O
JVM	B-Language
or	O
other	O
environment	O
(	O
such	O
as	O
Clojure	B-Language
and	O
Scala	B-Application
)	O
,	O
and	O
languages	O
that	O
do	O
not	O
have	O
a	O
standard	O
native	O
printf	B-Device
implementation	O
but	O
have	O
external	O
libraries	O
which	O
emulate	O
printf	B-Device
behavior	O
(	O
such	O
as	O
JavaScript	B-Language
)	O
.	O
</s>
<s>
The	O
utility	O
command	O
,	O
sometimes	O
built	O
in	O
to	O
the	O
shell	O
,	O
such	O
as	O
with	O
some	O
implementations	O
of	O
the	O
KornShell	B-Language
(	O
ksh	O
)	O
,	O
Bourne	B-Operating_System
again	I-Operating_System
shell	I-Operating_System
(	O
bash	B-Operating_System
)	O
,	O
or	O
Z	B-Application
shell	I-Application
(	O
zsh	B-Application
)	O
.	O
</s>
<s>
These	O
commands	O
usually	O
interpret	O
C	B-Language
escapes	I-Language
in	O
the	O
format	B-Language
string	I-Language
.	O
</s>
