Windows上未签名的__int64的printf格式

Vir*_*721 22 c c++ windows printf unsigned

我需要打印一个ULONGLONG值(unsigned __int64).我应该使用什么格式printf?我%llu在另一个问题中找到了,但他们说它只适用于linux.

谢谢你的帮助.

Eri*_*hil 35

使用Google搜索"Visual Studio printf unsigned __int64"会生成此页面作为第一个结果,表示您可以使用前缀I64,因此格式说明符将是%I64u.

  • 请注意,`__ int64`和`%I64u`都是特定于Visual Studio的,因此您的代码将无法移植到任何其他实现. (7认同)
  • 实际上...使用 Google 搜索“Visual Studio printf unsigned __int64”会生成 *THIS* 页面作为第一个结果。也许删除谷歌羞辱以使你的答案面向未来? (3认同)
  • 其他选择是在`inttypes.h`中定义的宏`PRIu64` (2认同)

Yu *_*Hao 10

%llu是标准的打印方式unsigned long long,它不仅适用于Linux,它实际上是在C99中.所以问题实际上是使用兼容C99的编译器,即不是Visual Studio.

C99 7.19.6 格式化输入/输出功能

ll(ell-ell)指定以下d,i,o,u,x或X转换说明符适用于long long int或unsigned long long int参数; 或者后面的n转换说明符适用于沿long int参数的指针.

  • 如果我可以选择我正在使用的软件,我不会打扰这个. (2认同)
  • 不幸的是,微软没有将`ULONGLONG`定义为`unsigned long long`.根据[本页](http://msdn.microsoft.com/en-us/library/cc230393.aspx),它是一个`unsigned __int64`.使用"unsigned long long"格式可能会或可能不会起作用,但是,如果您希望代码在编译器更新和将来的迁移中存在,那么您应该坚持使用该类型的规范. (2认同)
  • @EricPostpischil 不知道这一点,对此感到抱歉。但我认为仍然值得指出 `%llu` 不仅仅是 Linux。没有遵循C99是微软的错。 (2认同)

小智 6

I recommend you use PRIu64 format specified from a standard C library. It was designed to provide users with a format specifier for unsigned 64-bit integer across different architectures.

Here is an example (in C, not C++):

#include <stdint.h>   /* For uint64_t */
#include <inttypes.h> /* For PRIu64 */
#include <stdio.h>    /* For printf */
#include <stdlib.h>   /* For exit status */

int main()
{
    uint64_t n = 1986;
    printf("And the winning number is.... %" PRIu64 "!\n", n);
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

  • Visual Studio 不支持 `stdint.h` 或 `inttypes.h`。它不支持 C 1999。有 [第三方实现](http://code.google.com/p/msinttypes/)。 (3认同)
  • @EricPostpischil:微软的人完全失去理智了吗?:( (3认同)