If you don’t know about the %i and %d format specifiers, follow this article’s guidelines.
%i Format Specifier
In C programming language, the %i is used to print out a signed integer when used with the printf() function. The signed integers are the ones that can either be positive or negative. In C programming when using of %i the argument value that is passing must be an expression or an integer type which automatically evaluates in an integer datatype. You can also use the %i specifier to read a character value that comes within the range of a signed integer.
%d Format Specifier
The %d format specifier on the other hand is used to print out the unsigned integer in the C programming language. Here unsigned integers are positive integers. In C language when you use %d then the value of the arguments type int.
Differences
Both %i and %d format specifiers are frequently used in the C programming language for printing integers. They may look similar in terms of use as well as behave the same when using the printf() function.
Let’s follow up with a sample code where both format specifiers are used.
In this code we firstly initialize a variable and take value from the user using scanf(). Then we show output using %d and %i.
Output:
Both provide a similar output when used with the printf() function. It doesn’t matter whether you enter the positive value or negative, the result will always be the same.
However, if we look at the scanf function, we can differentiate them correctly. In the above code, if we input a decimal value like “012”. If we use “%d” instead of %i, you will get the output 12. The reason is that %d ignores the 0 zero value from the user input.
Output
If the same value is entered using %i as in scanf() function, the output will be 10 in this case. The reason is %i prints the decimal value of 012 (octal representation).
Conclusion
Format specifier is a pattern that initially starts with the % sign and it usually tells us what kind of data is placed in input and what data is to be shown. %d and %i are two specifiers widely used in C programming. They are similar when used with the printf() function but they output different results when used with the scanf() function.