3
string.format (formatstring, ···)

Returns a formatted version of its variable number of arguments following the description given in its first argument (which must be a string). The format string follows the same rules as the ISO C function sprintf. The only differences are that the options/modifiers *, h, L, l, n, and p are not supported and that there is an extra option, q.

Lua 5.3 doesn't support lld, how can I print lld in Lua 5.3?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sleepwom
  • 227
  • 6
  • 15
  • The question was modified to a different one by you. In C, `long long`, `unsigned long long`, and `uint64` are three different types, it's not clear what you are asking. I have rolled it back. If you have a new question, ask a new question. – Yu Hao Mar 14 '15 at 14:13

1 Answers1

3

Short answer: use %d.


In C sprintf, %lld is used to format a long long type, which is an integer type at least 64 bit.

In Lua 5.3, the type number has two internal representations, integer and float. Integer representation is 64-bit in standard Lua. You can use %d to print it no matter its internal representation:

print(string.format("%d", 2^62))

Output: 4611686018427387904


In Lua source file luaconf.h, you can see that Lua converts %d to the approapriate format:

#define LUA_INTEGER_FMT     "%" LUA_INTEGER_FRMLEN "d"

and LUA_INTEGER_FRMLEN is defined as "", "l" or "ll" if different internal representation for integer is used:

#if defined(LLONG_MAX)      /* { */
/* use ISO C99 stuff */
#define LUA_INTEGER     long long
#define LUA_INTEGER_FRMLEN  "ll"
//...
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • bad argument #2 to 'format' (number has no integer representation) – Sleepwom Mar 14 '15 at 07:53
  • print(string.format("%d", 9223372036854775807)) assert( 2^63 == 9223372036854775807 ) print(string.format("%d", 2^63)) -- bad argument #2 to 'format' (number has no integer representation) why? – Sleepwom Mar 14 '15 at 08:32
  • @Sleepwom The biggest number that can be represented in a signed 64-bit is `2^63 - 1`, read [Wikipedia](http://en.wikipedia.org/wiki/Integer_%28computer_science%29). – Yu Hao Mar 14 '15 at 08:50
  • how to print unsigned 64-bit? – Sleepwom Mar 14 '15 at 10:01
  • Use `string.format( "%u", 1 << 63 )`. Don't use the `^` operator to create large integers (because it doesn't create integers but floats). – siffiejoe Mar 14 '15 at 12:05