在OS X上的printf和%llu对%lu [英] printf and %llu vs %lu on OS X
问题描述
可能重复:
如何printf uint64_t?
为什么我在64位的Mac(我使用Clang) uint64_t
类型是 unsigned long long
uint64_t
类型是 unsigned long
?
Why is it that on my 64-bit Mac (I am using Clang) the uint64_t
type is unsigned long long
while on 64-bit Ubuntu the uint64_t
type is unsigned long
?
这让我很难得到我的 printf
调用不给编译器警告工作)。
This makes it very difficult for me to get my printf
calls to not give compiler warnings (or indeed even to work) under both environments.
我可以尝试使用宏尝试选择正确的字符串( #define LU
c>%llu 或%lu
,并且在处理丑陋的 printf
在Mac上我有一个64位字大小(因此 _LP64
将被定义, UINTPTR_MAX!= 0xffffffff
),但它仍然使用 long long
用于64位int类型。
I can try to use macros to try to choose the correct string (#define LU
either %llu
or %lu
, and in the process uglifying the printf
strings a bit) but on the Mac I've got a 64-bit word size (so _LP64
would be defined and UINTPTR_MAX != 0xffffffff
) and yet it still uses long long
for the 64 bit int types.
// printf macro switch (for the uint64_t's)
#if UINTPTR_MAX == 0xffffffff
// 32-bit
# define LU "%llu"
#else
// assume 64-bit
// special case for OS X because it is strange
// should actually check also for __MACH__
# ifdef __APPLE__
# define LU "%llu"
# else
# define LU "%lu"
# endif
#endif
推荐答案
在< cinttypes>
。尝试
printf("%"PRIu64, x);
或者更好的是使用C ++的功能,如
Or, even better, use C++ features like
std::cout << x;
这将选择正确的<您的变量类型的运算符。
which will select the proper << operator for your variable type.
这篇关于在OS X上的printf和%llu对%lu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!