Home > Backend Development > C++ > What\'s the Key Difference Between `%i` and `%d` in C\'s Formatted Input/Output?

What\'s the Key Difference Between `%i` and `%d` in C\'s Formatted Input/Output?

Mary-Kate Olsen
Release: 2024-11-28 15:23:10
Original
812 people have browsed it

What's the Key Difference Between `%i` and `%d` in C's Formatted Input/Output?

Difference between Conversion Specifiers %i and %d in Formatted IO Functions

In formatted IO functions, such as printf and scanf, conversion specifiers are used to control the interpretation and formatting of input and output data. Among the various specifiers, %i and %d are frequently used for integers.

Output:

When used for output (e.g., with printf), %i and %d behave identically. They both represent an integer value and print it in the default format based on the system locale.

Input:

However, the distinction between %i and %d emerges during input operations (e.g., with scanf). Here, their behavior differs:

  • %d: Scans an integer as a signed decimal number, represented in the basic arithmetic notation.
  • %i: Primarily scans integers as signed decimal numbers, but it also allows hexadecimal and octal representations if they are prefixed appropriately. Hexadecimal numbers begin with "0x" (e.g., "0x20"), while octal numbers begin with "0" (e.g., "025").

Therefore, in the case of an input like "033", scanf("%d") interprets it as 33 (a signed decimal number), while scanf("%i") interprets it as 27 (an octal number), indicating a difference in representation.

The above is the detailed content of What\'s the Key Difference Between `%i` and `%d` in C\'s Formatted Input/Output?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template