Difference between %d and %i format specifier in C language

3

A format specifier is a sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from the stream and stored into the locations pointed by the additional arguments. In short it tell us which type of data to store and which type of data to print.

For example – If we want to read and print integer using scanf() and printf() function, either %i or %d is used but there is subtle difference in both %i and %d format specifier.

%d specifies signed decimal integer while %i specifies integer.

%d and %i behave similar with printf

There is no difference between the %i and %d format specifiers for printf. Consider a following example.

// C program to demonstrate
// the behavior of %i and %d 
// with printf statement
#include <stdio.h>

int main()
{
	int num = 9;
	
	// print value using %d
	printf("Value of num using %%d is = %d\n", num);
	
	// print value using %i
	printf("Value of num using %%i is = %i\n", num);

	return 0;
}
Output:
Value of num using %d is = 9
Value of num using %i is = 9

%d and %i behavior is different in scanf

%d assume base 10 while %i auto detects the base. Therefore, both specifiers behaves differently while they are used with an input specifier. So, 012 would be 10 with %i but 12 with %d.

  • %d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value. Consider a following example.
  • %i takes integer value as integer value with decimal, hexadecimal or octal type.
    To enter a value in hexadecimal format – value should be provided by preceding “0x” and value in octal format – value should be provided by preceding “0”.

Consider a following example.

// C program to demonstrate the difference
// between %i and %d specifier
#include <stdio.h>

int main()
{
	int a, b, c;

	printf("Enter value of a in decimal format:");
	scanf("%d", &a);

	printf("Enter value of b in octal format: ");
	scanf("%i", &b);

    printf("Enter value of c in hexadecimal format: ");
	scanf("%i", &c);

	printf("a = %i, b = %i, c = %i", a, b, c);

	return 0;
}
Output:
Enter value of a in decimal format:12
Enter value of b in octal format: 012
Enter value of c in hexadecimal format: 0x12
a = 12, b = 10, c = 18

Explanation:
The decimal value of a as 12 is 12
The decimal value of b as 12(octal) is 10
The decimal value of c as 12(hexadecimal) is 18

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.