Warning when using qint64 with PRIi64 on 64 bit linux
-
Hi,
when I try to print a qint64 with printf and the format specifier PRIi64 on a 64 bit linux I get the following warning:
warning: format ‘%li’ expects argument of type ‘long int’, but argument 4 has type ‘qint64 {aka long long int}’ [-Wformat=]
g++ --version: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
How can I print without warnings?
Thanks in advance!
-
Hi,
when I try to print a qint64 with printf and the format specifier PRIi64 on a 64 bit linux I get the following warning:
warning: format ‘%li’ expects argument of type ‘long int’, but argument 4 has type ‘qint64 {aka long long int}’ [-Wformat=]
g++ --version: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
How can I print without warnings?
Thanks in advance!
-
Hi,
when I try to print a qint64 with printf and the format specifier PRIi64 on a 64 bit linux I get the following warning:
warning: format ‘%li’ expects argument of type ‘long int’, but argument 4 has type ‘qint64 {aka long long int}’ [-Wformat=]
g++ --version: g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
How can I print without warnings?
Thanks in advance!
@DuBu Use ll instead of li, see http://www.cplusplus.com/reference/cstdio/printf/
-
Hi @DuBu
PRIi64 should return "lli" on a 64-bit system. So maybe you use a 32-bit system or (cross-)compiler.
-Michael.
@m.sue Yes, I thought so too. But it doesn't! Here's a snippet from inttypes.h:
# if __WORDSIZE == 64 # define __PRI64_PREFIX "l" # define __PRIPTR_PREFIX "l" # else # define __PRI64_PREFIX "ll" # define __PRIPTR_PREFIX # endif # define PRIi64 __PRI64_PREFIX "i"
And the if branch is active (i.e __WORDSIZE == 64 is true).
So PRIi64 returns "li" on my system. The compiler is the one I already mentioned. -
@m.sue Yes, I thought so too. But it doesn't! Here's a snippet from inttypes.h:
# if __WORDSIZE == 64 # define __PRI64_PREFIX "l" # define __PRIPTR_PREFIX "l" # else # define __PRI64_PREFIX "ll" # define __PRIPTR_PREFIX # endif # define PRIi64 __PRI64_PREFIX "i"
And the if branch is active (i.e __WORDSIZE == 64 is true).
So PRIi64 returns "li" on my system. The compiler is the one I already mentioned.Hi @DuBu
I have the same compiler and the same code as you in /usr/include/inttypes.h. But PRIi64 returns "lli". No idea what could be the difference :-/
Ah I just tested: I get the same error as you in main.cpp but in some sub-dll where I use all my PRI* strings everything works fine. Interesting.
-Michael.
-
@m.sue Yes, I thought so too. But it doesn't! Here's a snippet from inttypes.h:
# if __WORDSIZE == 64 # define __PRI64_PREFIX "l" # define __PRIPTR_PREFIX "l" # else # define __PRI64_PREFIX "ll" # define __PRIPTR_PREFIX # endif # define PRIi64 __PRI64_PREFIX "i"
And the if branch is active (i.e __WORDSIZE == 64 is true).
So PRIi64 returns "li" on my system. The compiler is the one I already mentioned. -
@DuBu Use ll instead of li, see http://www.cplusplus.com/reference/cstdio/printf/
-
I wonder what is wrong, the qint64 which is actually a long long int or the PRIi64 macro which is actually a "li"?
-
Hi @DuBu
Qt could fix it by defining
typedef int64_t qint64;
instead oftypedef long long qint64;
on Linux. But, of course, there could be some side effects of that change that I do not see. Maybe some supported compiler does not yet have theint64_t
type.-Michael.
-
Hi @DuBu
Qt could fix it by defining
typedef int64_t qint64;
instead oftypedef long long qint64;
on Linux. But, of course, there could be some side effects of that change that I do not see. Maybe some supported compiler does not yet have theint64_t
type.-Michael.
@m.sue long long is not necessarily always 64 bit. The length of integral types in C/C++ isn't specified exactly. The only thing specified is:
char <= short <= int <= long int <= long long
The exact size of each type depends on platform and compiler.
-
@m.sue long long is not necessarily always 64 bit. The length of integral types in C/C++ isn't specified exactly. The only thing specified is:
char <= short <= int <= long int <= long long
The exact size of each type depends on platform and compiler.
Hi @jsulm
Ok, but as
int64_t
is guarantueed to be 64-bit, it is not guarantueed to be defined for every compiler. So it's no good choice forqint64
.Nevertheless Qt should find a way to use it without warnings in printf with the new PRI macros, They are just about invented to use with
long long
s that are or are not 64-bit depending on the compiler/machine.-Michael.
-
Hi @jsulm
Ok, but as
int64_t
is guarantueed to be 64-bit, it is not guarantueed to be defined for every compiler. So it's no good choice forqint64
.Nevertheless Qt should find a way to use it without warnings in printf with the new PRI macros, They are just about invented to use with
long long
s that are or are not 64-bit depending on the compiler/machine.-Michael.
@m.sue Well, qint64 is guaranteed to be 64 bit, so you cannot use long long to typedef it.
int64_t is defined in C++11 and as Qt requires C++11 since quite some time it is perfectly valid to use int64_t in my opinion.
And why use printf in C++ at all? -
@m.sue Well, qint64 is guaranteed to be 64 bit, so you cannot use long long to typedef it.
int64_t is defined in C++11 and as Qt requires C++11 since quite some time it is perfectly valid to use int64_t in my opinion.
And why use printf in C++ at all?