Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

%d and %i

[0]
Format Specifier %d
The format specifier %d takes integer value as a signed decimal integer value
which means values should be decimal whether it is negative or positive.

Format Specifier %i
The format specifier %i takes integer value as an integer value which means 
values should be decimal, octal and hexadecimal and octal value is provided by
preceding ‘0’ while hexadecimal value is provided by preceding ‘0x’.

[1]
They are the same when used for output, e.g. with printf.

However, these are different when used as input specifier e.g. with scanf, 
where %d scans an integer as a signed decimal number, but %i 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.

[2]
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.
 
PREVIOUS NEXT
Tagged:
ADD COMMENT
Topic
Name
6+6 =