简体   繁体   中英

printf format for unsigned __int64 on Windows

I need to print a ULONGLONG value ( unsigned __int64 ). What format should i use in printf ? I found %llu in another question but they say it is for linux only.

Thanks for your help.

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

%llu is the standard way to print unsigned long long , it's not just for Linux, it's actually in C99. So the problem is actually to use a C99-compatible compiler, ie, not Visual Studio.

C99 7.19.6 Formatted input/output functions

ll(ell-ell) Specifies that a following d, i, o, u, x, or X conversion specifier applies to a long long int or unsigned long long int argument; or that a following n conversion specifier applies to a pointer to along long int argument.

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;
}

Printf has different format specifiers for unsigned long long depending on the compiler, I have seen %llu and %Lu . In general I would advice you to use std::cout and similar instead.

这是HEX输出的解决方法

printf("%08X%08X", static_cast<UINT32>((u64>>32)&0xFFFFFFFF), static_cast<UINT32>(u64)&0xFFFFFFFF));

For guys who forget all the time like me,
If you use Visual Studio (MSVC),

%I64u for uint64_t (unsigned __int64)

%I64d for int64_t (__int64)

%Iu for size_t

You should check this MSDN for the details, or just this section :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM