Is there any difference between `printf("%d", x);`

and `printf("%i", x);`

?
I know the two return the same result, does it have some sort of convention adopted to always use `%d`

?

24

`printf("%d", x);`

and `printf("%i", x);`

?
I know the two return the same result, does it have some sort of convention adopted to always use `%d`

?

asked by Taisbevalle 12.09.2016 в 20:41

source
13

Conversion specifiers `%i`

and `%d`

are interpreted in the same way by the `fprintf()`

family functions, however, they are interpreted differently by the `fscanf()`

function family.

Both are present in all patterns: `C89`

, `C90`

, `C99`

and `C11`

.

`%d`

is used exclusively with integer decimal numbers, since `%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`

: Octal Predicate

`0x7B`

: Hexadecimal Predicate

The integers exemplified above will all be interpreted as the decimal `123`

.

I hope I have helped!

answered by 12.09.2016 / 21:26

source
25

No difference, will produce exactly the same result.

The difference occurs in `scanf()`

and its variations. The `%d`

only allows input of a signed integer in decimal format. The `%i`

allows the entry in hexadecimal or octal format.

The function of `scanf()`

is to receive typing characters, so it always does not receive numbers. What it does is analyze these characters and based on criteria try to convert them to numbers.

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

When using `%d`

the accepted pattern determines that characters only numeric digits and aggregate symbols, notably the negative sign.

In the `%i`

it is possible to include a prefix indicating that the input format is another (without a prefix the decimal is used), for example `0x`

, the letters from `a`

to `f`

, no matter the box, are also accepted, since the hexadecimal notation allows them.

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

Keep in mind that the input and output data are numeric representations and not the numbers themselves .

To use this format the buffer variable must be any integer type.

Before using `scanf()`

understand How to read from stdin in C?

answered by
12.09.2016 в 20:44

16

In% with%, both are equivalent (print an integer in base 10). In `printf`

, `scanf`

will interpret the number differently if it is preceded by `%i`

(interpretation as hexadecimal).

answered by
12.09.2016 в 20:44