1

My friend and I were arguing about %lli and %lld. I generally use %lli, and every time he argues to use %lld in printf and scanf. He claims %lli and %lld are different.

Is there any difference between the %lli and %lld format specifiers in GNU GCC compilers, or are they the same?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mohaimin66
  • 103
  • 1
  • 1
  • 8
  • According to [this `printf` (and family) reference](http://en.cppreference.com/w/c/io/fprintf) they're not different. – Some programmer dude Aug 21 '17 at 07:08
  • http://www.nullstone.com/download/releasenotes-ns-c.htm Release 4.8 Implement C99 long long int format specifier: Use the C99 long long int format specifier "%lld" when printing long long int values. – Hariom Singh Aug 21 '17 at 07:11
  • Note that the properties of `printf()` and `scanf()` are more a question of what the library does than what the compiler does, though it is increasingly complex. – Jonathan Leffler Aug 21 '17 at 07:30

2 Answers2

7

For printf, they are exactly the same.

http://en.cppreference.com/w/cpp/io/c/fprintf

d, i converts a signed integer into decimal representation [-]dddd.

Precision specifies the minimum number of digits to appear. The default precision is 1. If both the converted value and the precision are ​0​ the conversion results in no characters.


For scanf, they are different. Below is quote from documentation.

d matches a decimal integer. The format of the number is the same as expected by strtol() with the value 10 for the base argument

i matches an integer. The format of the number is the same as expected by strtol() with the value ​0​ for the base argument (base is determined by the first characters parsed)

In case of i, if your number starts with 0, it will be parsed as octal.

http://en.cppreference.com/w/cpp/io/c/fscanf

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
  • so which one should be preferred in which case? – Hariom Singh Aug 21 '17 at 07:15
  • 3
    @HariomSingh if you can expect number in other base than 10, use `i`. – unalignedmemoryaccess Aug 21 '17 at 07:16
  • 1
    @HariomSingh You should use `d` by default unless you really need automatic base detection. With `i` numbers like `012` are read as octal, which is counter intuitive, considering how rare they are these days. – user694733 Aug 21 '17 at 07:21
  • 2
    @HariomSingh: Note that dates are represented in base 10, so a date like 08/08/2017 or 09/09/2017 causes conniptions if you use `%i` and `scanf()` (because neither 8 nor 9 is an octal digit, so the 0 is one octal number, and the 8 or 9 is the next number). – Jonathan Leffler Aug 21 '17 at 07:25
3

Possibly you should see documentation...?

For printf() they are equivalent, but they differ for scanf().

http://www.cplusplus.com/reference/cstdio/scanf/

For specifier d, characters extracted from input stream are any number of decimal digits (0-9), optionally preceded by a sign (+ or -).

For i it's any number of digits, optionally preceded by a sign (+ or -).
Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
Signed argument.

CiaPan
  • 9,381
  • 2
  • 21
  • 35