Difference between %i and %d

Asked

Viewed 13,759 times

31

Is there any difference between printf("%d", x); and printf("%i", x);? I know the two return the same result, have some kind of convention adopted to always use the %d?

3 answers

29

No difference, will produce exactly the same result.

The difference occurs in the scanf() and its variations. The %d only allows input of an integer with signal in decimal format. The %i allows entry in hexadecimal or octal format.

The function of scanf() is to receive the typing of characters, always so, he does not receive numbers. What he does is parse these characters and based on criteria, try to convert them to numbers.

Therefore its use alone is very little useful. In general in exercises in C or very simple things, which probably should not even be done in C, it is used in a naive way and for this purpose there is no big problem. It is common for people to have other means of data entry into production systems. Variations read other data sources that have more control and may be that the content is guaranteed to be valid. Note what happens when you type something invalid.

When using the %d the default determines that characters that are only numeric digits and aggregate symbols, notably the negative sign.

Already in the %i it is possible to include a prefix indicating that the input format is another (no prefix is used decimal), then use, for example 0x, the letters of a until f, no matter the box, are accepted as well, since the hexadecimal notation allows them.

Of course it is not just a question of which characters are accepted, the conversion algorithm is also different to produce the expected number.

Remember that the data input and output are numerical representations and not the numbers themselves.

To use this format the variable of buffer must be a whole guy.

Before using the scanf() understand How to read from stdin in C?

Documentation

20


The conversion specifiers %i and %d are interpreted in the same way by the functions of the family fprintf(), however, they are interpreted differently by the function family of the fscanf().

Both are present in all patterns: C89, C90, C99 and C11.

%d is used exclusively with integer decimal numbers, whereas the %i is only used for integers (regardless of whether the base is octal, decimal or hexadecimal).

The %i in a fscanf() is able to differentiate integers by predicates, for example:

123: Decimal

0173: Predicado Octal

0x7B: Hexadecimal Predicate

The above integers will all be interpreted as decimal 123.

  • 1

    C90 ? I think it should be C99...

  • I believe this has existed since the beginning of language. I did a test on GCC, and I was able to compile programs containing the %i with the flags -std=c89 and -Wpedantic setados, no errors or warnings.

  • 2

    Lacobus, what @Joséx. meant is that there is no C90. There is C99.

  • 1

    Wikipedia: https://en.wikipedia.org/wikiANSI_C#C90

  • 1

    GCC Handbook, flag -std=: https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html

  • 2

    Wikipedia: The same standard as C89 was ratified by the International Organization for Standardization as ISO/IEC 9899:1990, with only formatting changes,[1] which is sometimes referred to as C90. Therefore, the Terms "C89" and "C90" refer to essentially the same language.

  • 2

    Wikipedia, same article: This standard has been Withdrawn by Both ANSI/INCITS and ISO/IEC...well, I’ve never seen anyone reference that pattern, I’ve always seen C89, C99, and now C11

Show 2 more comments

18

In the printf, both are equivalent (print an integer in base 10). In scanf, the %i shall interpret the different number if it is preceded by 0x (interpretation as hexadecimal).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.