Difference between %d and %i format specifier in C

CProgrammingServer Side Programming

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.

Here is an example of format specifier %d in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int v1 = 7456;
   int v2 = -17346;
   printf("The value in decimal form : %d\n", v1);
   printf("The value in negative : %d", v2);
   return 0;
}

Output

The value in decimal form : 7456
The value in negative : -17346

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’.

Here is an example of format specifier %i in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   int v1 = 1327;
   int v2 = 0x42451;
   printf("The value in decimal form : %d\n", v1);
   printf("The value in hexadecimal form : %i", v2);
   return 0;
}

Output

The value in decimal form : 1327
The value in hexadecimal form : 271441
raja
Published on 08-Oct-2018 11:35:38
Advertisements