printf format for unsigned __int64 on Windows

旧巷老猫 提交于 2019-12-18 11:44:56

问题


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.


回答1:


Using Google to search for “Visual Studio printf unsigned __int64” produces this page as the first result, which says you can use the prefix I64, so the format specifier would be %I64u.




回答2:


%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, i.e, 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.




回答3:


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



回答4:


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.




回答5:


Here is a work around for HEX output

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


来源:https://stackoverflow.com/questions/18107426/printf-format-for-unsigned-int64-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!