What Are The Differences Between %d And %i Format Specifier

Difference between %d and %i format specifier

  • %d and %i are both used to read and print integer values but they still have differences.
  • %d - Specifies signed decimal integer.
  • %i - Specifies an integer.

%d and %i as output specifiers

There are no differences between %d and %i format specifier. If both the specifiers are same, they are being used as the output specifiers, printf function will print the same value; either %d or %i is used. 

  1. #include < stdio.h >   
  2. #include < conio.h > int main() {  
  3.     int a = 13456;  
  4.     clrscr();  
  5.     printf("value of \"a\" using %%d is= %d\n", a);  
  6.     printf("value of \"a\" using %%i is= %i\n", a);  
  7.     getch();  
  8.     return 0;  
  9. }   

Output

value of a using %d is= 13456

value of a using %d is= 13456

Sample code Screen

 

Sample output Screen

 

  • %d and %i as Input specifiers
    There are differences between %d and %i format specifier. Both specifiers are different, if they are using input specifiers, scanf function will act differently based on %d and %. 
  • %d as input specifier
    %d takes an integer value as signed decimal integer i.e. it takes negative values along with the positive values but the values should be decimal.
  • %i as input specifier
    %i takes an integer value as an integer value with a decimal, hexadecimal or an octal type.

To give the value in hexadecimal format - the value should be provided by preceding "0x" and the value in an octal format - value should be provided by preceding "0". 

  1. #include < stdio.h >   
  2. #include < conio.h > int main()  
  3. {  
  4.     int a = 0, b = 0;  
  5.     clrscr();  
  6.     printf("enter the value of a :");  
  7.     scanf("%d", & a); //reading a value from console  
  8.     printf("enter the value of b :");  
  9.     scanf("%i", & b); //reading a value from console  
  10.     printf("value of \"a\" using %%d is= %d\n", a);  
  11.     printf("value of \"b\" using %%i is= %i\n", b);  
  12.     getch();  
  13.     return 0;  
  14. }   

Output

Enter the value of a :65

Enter the value of a :065 //little change in my input I'll take octal number here .

Value of using %d is= 65

Value of using %d is= 53 // Also I got little difference in my output. decimal number of 65 is 53

Sample code screen

 

Sample output screen

Thanks for reading.