Printf - C++ Reference
Printf - C++ Reference
printfC++Reference
Search:
Reference
Go
<cstdio>
printf
Notloggedin
register
login
C++
YouareusingaversionwithoutAdsofthiswebsite.Please,considerdonating:
Information
Tutorials
Reference
Articles
Forum
[hide]
Reference
Clibrary:
<cassert>(assert.h)
<cctype>(ctype.h)
<cerrno>(errno.h)
<cfenv>(fenv.h)
<cfloat>(float.h)
<cinttypes>(inttypes.h)
<ciso646>(iso646.h)
<climits>(limits.h)
<clocale>(locale.h)
<cmath>(math.h)
<csetjmp>(setjmp.h)
<csignal>(signal.h)
<cstdarg>(stdarg.h)
<cstdbool>(stdbool.h)
<cstddef>(stddef.h)
<cstdint>(stdint.h)
<cstdio>(stdio.h)
<cstdlib>(stdlib.h)
<cstring>(string.h)
<ctgmath>(tgmath.h)
<ctime>(time.h)
<cuchar>(uchar.h)
<cwchar>(wchar.h)
<cwctype>(wctype.h)
Containers:
Input/Output:
Multithreading:
Other:
<cstdio>(stdio.h)
functions:
clearerr
fclose
feof
ferror
fflush
fgetc
fgetpos
fgets
fopen
fprintf
fputc
fputs
fread
freopen
fscanf
fseek
fsetpos
ftell
fwrite
getc
getchar
gets
perror
printf
putc
putchar
puts
remove
rename
rewind
scanf
setbuf
setvbuf
snprintf
sprintf
sscanf
tmpfile
tmpnam
ungetc
vfprintf
function
printf
<cstdio>
intprintf(constchar*format,...);
Printformatteddatatostdout
WritestheCstringpointedbyformattothestandardoutput(stdout).Ifformatincludesformatspecifiers(subsequences
beginningwith%),theadditionalargumentsfollowingformatareformattedandinsertedintheresultingstringreplacing
theirrespectivespecifiers.
Parameters
format
Cstringthatcontainsthetexttobewrittentostdout.
Itcanoptionallycontainembeddedformatspecifiersthatarereplacedbythevaluesspecifiedinsubsequent
additionalargumentsandformattedasrequested.
Aformatspecifierfollowsthisprototype:[seecompatibilitynotebelow]
%[flags][width][.precision][length]specifier
Wherethespecifiercharacterattheendisthemostsignificantcomponent,sinceitdefinesthetypeandthe
interpretationofitscorrespondingargument:
specifier
Output
Example
dori
Signeddecimalinteger
392
u
Unsigneddecimalinteger
7235
o
x
X
f
F
e
E
Unsignedoctal
Unsignedhexadecimalinteger
Unsignedhexadecimalinteger(uppercase)
Decimalfloatingpoint,lowercase
Decimalfloatingpoint,uppercase
Scientificnotation(mantissa/exponent),lowercase
Scientificnotation(mantissa/exponent),uppercase
610
7fa
7FA
392.65
392.65
3.9265e+2
3.9265E+2
g
G
a
A
c
s
Usetheshortestrepresentation:%eor%f
Usetheshortestrepresentation:%Eor%F
Hexadecimalfloatingpoint,lowercase
Hexadecimalfloatingpoint,uppercase
Character
Stringofcharacters
392.65
392.65
0xc.90fep2
0XC.90FEP2
a
sample
Pointeraddress
b8000000
Nothingprinted.
Thecorrespondingargumentmustbeapointertoasignedint.
Thenumberofcharacterswrittensofarisstoredinthepointedlocation.
A%followedbyanother%characterwillwriteasingle%tothestream.
%
n
%
Theformatspecifiercanalsocontainsubspecifiers:flags,width,.precisionandmodifiers(inthatorder),whichare
optionalandfollowthesespecifications:
flags
description
LeftjustifywithinthegivenfieldwidthRightjustificationisthedefault(seewidthsubspecifier).
Forcestopreceedtheresultwithaplusorminussign(+or)evenforpositivenumbers.Bydefault,only
+
negativenumbersareprecededwithasign.
(space) Ifnosignisgoingtobewritten,ablankspaceisinsertedbeforethevalue.
Usedwitho,xorXspecifiersthevalueispreceededwith0,0xor0Xrespectivelyforvaluesdifferentthan
zero.
#
Usedwitha,A,e,E,f,F,gorGitforcesthewrittenoutputtocontainadecimalpointevenifnomore
digitsfollow.Bydefault,ifnodigitsfollow,nodecimalpointiswritten.
0
Leftpadsthenumberwithzeroes(0)insteadofspaceswhenpaddingisspecified(seewidthsubspecifier).
width
(number)
*
description
Minimumnumberofcharacterstobeprinted.Ifthevaluetobeprintedisshorterthanthisnumber,the
resultispaddedwithblankspaces.Thevalueisnottruncatedeveniftheresultislarger.
Thewidthisnotspecifiedintheformatstring,butasanadditionalintegervalueargumentpreceding
theargumentthathastobeformatted.
.precision
.number
http://www.cplusplus.com/reference/cstdio/printf/
description
Forintegerspecifiers(d,i,o,u,x,X):precisionspecifiestheminimumnumberofdigitstobewritten.If
thevaluetobewrittenisshorterthanthisnumber,theresultispaddedwithleadingzeros.Thevalue
isnottruncatedeveniftheresultislonger.Aprecisionof0meansthatnocharacteriswrittenforthe
value0.
Fora,A,e,E,fandFspecifiers:thisisthenumberofdigitstobeprintedafterthedecimalpoint(by
default,thisis6).
ForgandGspecifiers:Thisisthemaximumnumberofsignificantdigitstobeprinted.
1/3
1/15/2015
printfC++Reference
Fors:thisisthemaximumnumberofcharacterstobeprinted.Bydefaultallcharactersareprinted
untiltheendingnullcharacterisencountered.
Iftheperiodisspecifiedwithoutanexplicitvalueforprecision,0isassumed.
vfscanf
vprintf
vscanf
vsnprintf
vsprintf
vsscanf
objects:
.*
Theprecisionisnotspecifiedintheformatstring,butasanadditionalintegervalueargument
precedingtheargumentthathastobeformatted.
Thelengthsubspecifiermodifiesthelengthofthedatatype.Thisisachartshowingthetypesusedtointerpret
thecorrespondingargumentswithandwithoutlengthspecifier(ifadifferenttypeisused,thepropertype
promotionorconversionisperformed,ifallowed):
specifiers
length
di
uoxX
fFeEgGaA
c
s
p
n
stderr
stdin
stdout
types:
FILE
fpos_t
size_t
macroconstants:
BUFSIZ
EOF
FILENAME_MAX
FOPEN_MAX
L_tmpnam
NULL
TMP_MAX
(none) int
unsignedint
double
int
char*
void* int*
hh
signedchar unsignedchar
signedchar*
h
shortint
unsignedshortint
shortint*
l
longint
unsignedlongint
wint_t wchar_t*
longint*
ll
longlongint unsignedlonglongint
longlongint*
j
intmax_t
uintmax_t
intmax_t*
z
size_t
size_t
size_t*
t
ptrdiff_t
ptrdiff_t
ptrdiff_t*
L
longdouble
Noteregardingthecspecifier:ittakesanint(orwint_t)asargument,butperformstheproperconversiontoa
charvalue(orawchar_t)beforeformattingitforoutput.
Note:YellowrowsindicatespecifiersandsubspecifiersintroducedbyC99.See<cinttypes>forthespecifiersfor
extendedtypes.
...(additionalarguments)
Dependingontheformatstring,thefunctionmayexpectasequenceofadditionalarguments,eachcontaininga
valuetobeusedtoreplaceaformatspecifierintheformatstring(orapointertoastoragelocation,forn).
Thereshouldbeatleastasmanyoftheseargumentsasthenumberofvaluesspecifiedintheformatspecifiers.
Additionalargumentsareignoredbythefunction.
ReturnValue
Onsuccess,thetotalnumberofcharacterswrittenisreturned.
Ifawritingerroroccurs,theerrorindicator(ferror)issetandanegativenumberisreturned.
Ifamultibytecharacterencodingerroroccurswhilewritingwidecharacters,errnoissettoEILSEQandanegativenumber
isreturned.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*printfexample*/
#include<stdio.h>
Edit
&
intmain()
Run
{
printf("Characters:%c%c\n",'a',65);
printf("Decimals:%d%ld\n",1977,650000L);
printf("Precedingwithblanks:%10d\n",1977);
printf("Precedingwithzeros:%010d\n",1977);
printf("Somedifferentradices:%d%x%o%#x%#o\n",100,100,100,100,100);
printf("floats:%4.2f%+.0e%E\n",3.1416,3.1416,3.1416);
printf("Widthtrick:%*d\n",5,10);
printf("%s\n","Astring");
return0;
}
Output:
Characters:aA
Decimals:1977650000
Precedingwithblanks:1977
Precedingwithzeros:0000001977
Somedifferentradices:100641440x640144
floats:3.14+3e+0003.141600E+000
Widthtrick:10
Astring
Compatibility
Particularlibraryimplementationsmaysupportadditionalspecifiersandsubspecifiers.
ThoselistedherearesupportedbythelatestCandC++standards(bothpublishedin2011),butthoseinyellowwere
introducedinC99(onlyrequiredforC++implementationssinceC++11),andmaynotbesupportedbylibrariesthat
complywitholderstandards.
Seealso
puts
Writestringtostdout(function)
scanf
Readformatteddatafromstdin(function)
fprintf
Writeformatteddatatostream(function)
fwrite
Writeblockofdatatostream(function)
http://www.cplusplus.com/reference/cstdio/printf/
2/3
1/15/2015
printfC++Reference
Homepage|Privacypolicy
cplusplus.com,20002015Allrightsreservedv3.1
Spottedanerror?contactus
http://www.cplusplus.com/reference/cstdio/printf/
3/3