tags:

views:

402

answers:

4

what is the difference between %d and %i when used in printf function ?

+2  A: 

AFAIK, none. Both are int to/from decimal.

Pavel Radzivilovsky
+2  A: 

There isn't any - the two are synonyms.

anon
+13  A: 

These are identical for printf but different for scanf. For printf, both %d and %i designate a signed decimal integer. For scanf, %d and %i also means a signed integer but %i inteprets the input as a hexadecimal number if preceded by 0x and octal if preceded by 0 and otherwise interprets the input as decimal.

Jason
Out of curiosity, is there a reason why you dropped my answer from being the selected one?
Jason
+12  A: 

They are the same when used for output, e.g. with printf, but different when used as input specifier e.g. with scanf, where %d scans an integer as a signed decimal number, but %i allows defaults to decimal but also allows hexadecimal (if preceded by "0x") and octal if preceded by "0".

So "033" would be 27 with %i but 33 with %d.

Dipstick