Home > Software engineering >  Difference Between "%i" and "%d"
Difference Between "%i" and "%d"

Time:10-15

What is the difference between "%i" and "%d" in scanf function, are they same or different . "%d" is used to print integer value what is the use of "%i".

CodePudding user response:

With printf, you always want to specify the base to print in. So %d prints in decimal, and %x prints in hexadecimal, and %o prints in octal (base 8).

The formats for scanf generally mirror printf. So %d reads decimal, and %x reads hexadecimal, and %o reads octal.

Now, in a C source file, you can use any of the three bases, with a prefix indicating your choice: a leading 0x indicates hexadecimal, and a leading 0 indicates octal. That's for the C compiler reading C source files, but somebody realized it'd be nice if your program had the ability to read input in any base at run time. So, for example, the strtol function is normally told (via its third argument) exactly what base to accept, but if you give it a third argument of 0, it intuits the base based on a prefix, just like the compiler does.

Now we get to %i. Someone decided it'd be nice to give scanf the ability to read any base, just like strtol does. So that's what %i does: it, well, reads any base, paying attention to a leading 0 or 0x prefix, just like strtol does.

But then, finally, since it's nice if printf and scanf formats mirror each other, someone decided it would be good if printf accepted %i also. In a way it's meaningless: you obviously can't intuit an output format the same way you can an input format. So for printf, %i always means decimal, just like %d. So it's redundant and unnecessary, but I've noticed that more and more of the questions asked here on SO seem to use it, so someone out there most be teaching new programmers to use it.

CodePudding user response:

The format specification %i opposite to the conversion specification %d used in the family of functions like scanf can interpret the input as an octal number if it starts from 0 or as hexadecimal number if it is preceded by the hexadecimal suffix 0x or 0X.

From the C Standard (7.21.6.2 The fscanf function)

i Matches an optionally signed integer, whose format is the same as expected for the subject sequence of the strtol function with the value 0 for the base argument. The corresponding argument shall be a pointer to signed integer.

and (7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions)

3 If the value of base is zero, the expected form of the subject sequence is that of an integer constant as described in 6.4.4.1, optionally preceded by a plus or minus sign, but not including an integer suffix.

And integer constants are

(6.4.4.1) integer-constant:
    decimal-constant integer-suffixopt
    octal-constant integer-suffixopt
    hexadecimal-constant integer-suffixopt

(6.4.4.1) decimal-constant:
    nonzero-digit
    decimal-constant digit
(6.4.4.1) octal-constant:
    0
    octal-constant octal-digit
(6.4.4.1) hexadecimal-constant:
    hexadecimal-prefix hexadecimal-digit
    hexadecimal-constant hexadecimal-digit
(6.4.4.1) hexadecimal-prefix: one of
    0x 0X
  • Related