C语言 使用 printf 跨平台打印 64 位整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6299083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cross-platform printing of 64-bit integers with printf
提问by Andrei
In Windows, it is "%I64d". In Linux and Solaris, it is "%lld".
If I want to write cross-platform printfsthat prints long longvalues: what is good way of doing so ?
在 Windows 中,它是“%I64d”。在 Linux 和 Solaris 中,它是“%lld”。
如果我想编写printfs打印long long值的跨平台:这样做的好方法是什么?
long long ll;
printf(???, ll);
回答by DigitalRoss
There are a couple of approaches.
有几种方法。
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.)
您可以以符合 C99 的方式编写代码,然后在编译器编写者让您失望时提供特定于系统的技巧。(遗憾的是,这在 C99 中相当普遍。)
#include <stdint.h>
#include <inttypes.h>
printf("My value is %10" PRId64 "\n", some_64_bit_expression);
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 #definefor PRId64(or whatever) on that system.
如果你的目标系统中的一个已经被忽视落实<inttypes.h>或已在一些其他的方式恶魔般的懈怠,因为某些类型的功能是可选的,那么你只需要一个系统特定#define的PRId64(或其他)在该系统上。
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:
另一种方法是选择当前始终实现为 64 位并受 printf 支持的内容,然后进行转换。不完美,但通常会这样做:
printf("My value is %10lld\n", (long long)some_64_bit_expression);
回答by Random832
MSVC supports long longand llstarting Visual Studio 2005.
MSVC 支持long long并ll启动Visual Studio 2005。
You could check the value of the _MSC_VERmacro(>= 1400for 2005), or simply don't support older compilers.
您可以检查_MSC_VER宏的值(>= 1400对于 2005),或者干脆不支持旧的编译器。
It doesn't provide the C99 macros, so you will have to cast to long longrather than using PRId64.
它不提供 C99 宏,因此您必须强制转换为long long而不是使用PRId64.
This won't help if you're using older MSVC libraries with a non-MSVC compiler (I think mingw, at least, provides its own version of printf that supports ll)
如果您将旧的 MSVC 库与非 MSVC 编译器一起使用,这将无济于事(我认为 mingw 至少提供了它自己的支持 的 printf 版本ll)
回答by Jens Gustedt
No on linux and solaris it is only incidentally that this is lldfor a 64bit type. C99 prescribes simple (but ugly) macros to make these things portable PRId64. Since some windows compilers don't follow the standard you might be out of luck, there, unfortunately.
在 linux 和 solaris 上没有,只是顺便说一下这是lld针对 64 位类型的。C99 规定了简单(但丑陋)的宏来使这些东西可移植PRId64。不幸的是,由于某些 Windows 编译器不遵循标准,因此您可能不走运。
Edit:In your example you are using a different thing than a 64bit integer, namely a long long. This could well be 128 on some architectures. Here C99 has typedefs that guarantee you the minimum or exact width of the type (if they are implemented on the platform). These types are found with the inttypes.hheader, namely int64_tfor a fixe-width 64 bit type represented in two's complement. Maybe or maybe not your windows compiler has this.
编辑:在您的示例中,您使用的是与 64 位整数不同的东西,即 a long long。在某些架构上,这很可能是 128。这里 C99 有typedefs 保证你的类型的最小或精确宽度(如果它们在平台上实现)。这些类型与inttypes.h标头一起找到,即int64_t用于以二进制补码表示的固定宽度 64 位类型。也许或也许没有你的 Windows 编译器有这个。
回答by Aleksander Alekseev
As alternative you can use code like this:
作为替代方案,您可以使用这样的代码:
uint64_t currentTimeMs = ...;
printf("currentTimeMs = 0x%08x%08x\n",
(uint32_t)(currentTimeMs >> 32),
(uint32_t)(currentTimeMs & 0xFFFFFFFF)
);
Or maybe:
或者可能:
printf("currentTimeMs = %u%09u\n",
(uint32_t)(currentTimeMs / 1000000000),
(uint32_t)(currentTimeMs % 1000000000)
);

