You are on page 1of 2

5/19/2016

c++Differencebetweenformatspecifiers%iand%dinprintfStackOverflow

signup

StackOverflowisacommunityof4.7
millionprogrammers,justlikeyou,
helpingeachother.

login

tour

help

JointheStackOverflowcommunityto:

Jointhemitonlytakesaminute:
Askprogramming
questions

Signup

Answerandhelp
yourpeers

Getrecognizedforyour
expertise

Differencebetweenformatspecifiers%iand%dinprintf
Whatisthedifferencebetween
printf ?

%d

and

%i

whenusedasformatspecifiersin

c++ c format printf


editedSep18'11at15:08

askedDec12'09at13:49

jason
154k

Ayoub
20

288

431

1,586

28

42

4Answers

Theyarethesamewhenusedforoutput,e.g.with printf ,butdifferentwhenusedasinput


specifiere.g.with scanf ,where %d scansanintegerasasigneddecimalnumber,but %i
defaultstodecimalbutalsoallowshexadecimal(ifprecededby 0x )andoctalifprecededby
So

033

wouldbe27with

%i

but33with

%d

.
editedApr17at19:52

answeredDec12'09at13:58

taskinoor
29.7k

Dipstick
68

104

6,310

18

20

1 Expectinganintwithpossiblezeropaddinginsscanfseemstometobethemostreasonabledefault
behavior.Ifyou'renotexpectingOctal,thatcouldcausesubtlebugs.Sothissuggeststhat%disagood
specifiertousewhenyouhavetochooseonearbitrarily,unlessyouexplicitlywanttoreadoctaland/or
hex.EliotSep6'13at22:28

Theseareidenticalfor printf butdifferentfor scanf .For printf ,both %d and %i designatea


signeddecimalinteger.For scanf , %d and %i alsomeansasignedintegerbut %i intepretsthe
inputasahexadecimalnumberifprecededby 0x andoctalifprecededby 0 andotherwise
interpretstheinputasdecimal.
editedDec12'09at13:59

answeredDec12'09at13:53

jason
154k

20

288

431

Thereisn'tanyinthosewordsthetwoaresynonyms.
editedMar14'14at8:21

answeredDec12'09at13:52

hqt
9,745

anon
30

92

172

12 @hqt:whyshouldhe?Thisansweriscorrect.Idon'tgetyoupeople(downvoters),thequestionisabout
printf ,not scanf ,thereforetheseanswersarecorrect.Whydownvotethem? ybungalobill Jul11'12
at6:35

Thereisnodifferencebetweenthe %i and %d formatspecifiersfor printf .Wecanseethisby


goingtothedraftC99standardsection 7.19.6.1 Thefprintffunctionwhichalsocovers printf
withrespecttoformatspecifiersanditsaysinparagraph8:
Theconversionspecifiersandtheirmeaningsare:
andincludesthefollowingbullet:
d,iTheintargumentisconvertedtosigneddecimalinthestyle
[]dddd.Theprecisionspecifiestheminimumnumberofdigitsto

http://stackoverflow.com/questions/1893490/differencebetweenformatspecifiersianddinprintf

1/2

5/19/2016

c++Differencebetweenformatspecifiers%iand%dinprintfStackOverflow

appear;ifthevaluebeingconvertedcanberepresentedinfewer
digits,itisexpandedwithleadingzeros.Thedefaultprecisionis
1.Theresultofconvertingazerovaluewithaprecisionofzerois
nocharacters.

Ontheotherhandfor scanf thereisadifference, %d assumebase10while %i autodetects


thebase.Wecanseethisbygoingtosection 7.19.6.2 Thefscanffunctionwhichcovers scanf
withrespecttoformatspecifier,inparagraph12itsays:
Theconversionspecifiersandtheirmeaningsare:
andincludesthefollowing:
dMatchesanoptionallysigneddecimalinteger,whoseformatisthe
sameasexpectedforthesubjectsequenceofthestrtolfunctionwith
thevalue10forthebaseargument.Thecorrespondingargumentshall
beapointertosignedinteger.
iMatchesanoptionallysignedinteger,whoseformatisthesameas
expectedforthesubjectsequenceofthestrtolfunctionwiththe
value0forthebaseargument.Thecorrespondingargumentshallbea
pointertosignedinteger.

editedAug29'14at2:50

answeredAug28'14at20:07

ShafikYaghmour
97.3k

19

217

310

protectedbyRichardJ.RossIIIJun20'12at11:58
Thankyouforyourinterestinthisquestion.Becauseithasattractedlowqualityorspamanswersthathadtoberemoved,postingananswernowrequires10reputation
onthissite.
Wouldyouliketoansweroneoftheseunansweredquestions instead?

http://stackoverflow.com/questions/1893490/differencebetweenformatspecifiersianddinprintf

2/2

You might also like