问题描述
在Windows中,它是%I64d号。在Linux和Solaris,它是%LLD。结果
如果我想写打印长长值跨平台printfs输出。什么是这样做的好办法吗?
In Windows, it is "%I64d". In Linux and Solaris, it is "%lld".
If I want to write cross-platform printfs that print long long values. What is good way of doing so ?
long long ll;
printf(???, ll);
感谢
推荐答案
有几个方法。
您可以写在C99-符合时尚的code,然后提供系统特定的黑客编译的时候,作家让你失望。 (可悲的是,这是相当常见的C99。)
You could write your code in C99-conforming fashion, and then supply system-specific hacks when the compiler-writers let you down. (Sadly, that's rather common in C99.)
#include <stdint.h>
#include <inttypes.h>
printf("My value is %10" PRId64 "\n", some_64_bit_expression);
如果您的目标系统中的一个已经被忽视的实施&LT; inttypes.h&GT;
或者已经以其他方式恶魔般的懈怠,因为有些类型的特点是可选的,那么你只需要一个系统特定的的#define
为 PRId64
(或其他)在该系统上。
If one of your target systems has neglected to implement <inttypes.h>
or has in some other way fiendishly slacked off because some of the type features are optional, then you just need a system-specific #define
for PRId64
(or whatever) on that system.
另一种方法是捡东西,因此目前始终为64位的实现,是由printf的支持,再抹上。并不完美,但它通常会做的:
The other approach is to pick something that's currently always implemented as 64-bits and is supported by printf, and then cast. Not perfect but it will often do:
printf("My value is %10lld\n", (long long)some_64_bit_expression);
这篇关于64位整数的跨平台与印刷的printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!