English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

in C programming we can use both %d and %i for interger variable.
that is, both these give same output:
printf(" %d", x);
printf(" %i" , x);

whats the difference between %d and %i ??

2007-02-13 01:39:09 · 3 answers · asked by sh 1 in Computers & Internet Software

3 answers

Basics:
NO! %d & %i are not same. But they sure have similar functionality.
They are similar because, they are used to handle signed integer values.
But they are DIFFERENT because;while %d interprets the argument as decimal integer. %i handles decimal, octal or hexadecimal integer values.


Details:
Syntax of printf : int printf("", arg1,arg2....);
Control strings(also called format strings) contain specifications which direct the interpretation of input(in case of scanf)/output data.

When using decimal integers use %d.
Use %i when any one/all of decimal,octal, hexadecimal integers are to handled.Here '%' is the conversion character

Here area few others, which would help in further understanding the difference

%i=for decimal,octal or hexadecimal
%d=for decimal
%x=for hexadecimal
%o=for octal

I restrict myself only to the direct-n-concise answer to your question,yet dont restrict yourself to this only. There are a lot more fexibility provided by these functions.

2007-02-13 02:12:30 · answer #1 · answered by Basics1st, Details2nd 1 · 2 0

I don't think there's any difference. But %d is explicitely decimal, so use it if you want to make clear that it's a decimal number.

2007-02-13 02:04:27 · answer #2 · answered by nightgirl1200 4 · 0 0

None, really. They are both signed decimal integers.

2007-02-13 01:42:07 · answer #3 · answered by Linux OS 7 · 0 0

fedest.com, questions and answers