Home >Backend Development >C++ >%i vs. %d in C: When Do These Format Specifiers Differ?
Differences Between Format Specifiers %i and %d in Formatted I/O Functions (printf / scanf)
Formatted I/O functions like printf and scanf utilize conversion specifiers to control how data is represented during input or output. Among these specifiers, %i and %d are commonly used for integers.
Usage for Output (%i vs. %d)
When used as format specifiers for output, there is no distinction between %i and %d. Both produce the same result. They represent an integer value in decimal format.
Input Specifiers
However, the distinction emerges when using %i and %d as input specifiers (in scanf). Here's where the key difference lies:
%i: Default behavior is similar to %d (signed decimal integer). However, it has an additional feature: it allows input in other bases, such as:
Practical Example
To illustrate the difference, consider the input string "033".
In summary, while %i and %d behave similarly for output, they differ in their behavior for input. %i provides extended functionality by allowing hexadecimal and octal input, while %d is strictly limited to signed decimal input.
The above is the detailed content of %i vs. %d in C: When Do These Format Specifiers Differ?. For more information, please follow other related articles on the PHP Chinese website!