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.
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
.
PRIu64
and inttypes.h
are not supported in Visual Studio (at least, not by Microsoft).
Aug 7, 2013 at 16:45
__int64
and %I64u
are specific to Visual Studio, so your code will not be portable to any other implementation.
Aug 7, 2013 at 16:48
%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.
ULONGLONG
to be an unsigned long long
. Per this page, it is an unsigned __int64
. Using a format for unsigned long long
might or might not work, but, if you want your code to survive compiler updates and future migrations, you should stick to the specification of the type.
Aug 7, 2013 at 15:34
%llu
is not Linux only. It is Microsoft's fault not to follow C99.
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;
}
stdint.h
or inttypes.h
. It does not support C 1999. There are third-party implementations of them.
Aug 7, 2013 at 15:36
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.
std::cout
is nothing more than a syntax error.
Aug 7, 2013 at 16:53
Here is a work around for HEX output
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 (choosing MSVC compiler, to be specific),
%I64u
for uint64_t
== unsigned __int64
== unsigned long long
%I64d
for int64_t
== __int64
== long long
%Iu
for size_t
(==unsigned __int64
in win64, else unsigned int
)
You should check this MSDN for the details, or just this section :)
also, if interested, other MSDNs like this and this.
# C++ Windows format string MSVC Visual Studio size_t int64_t uint64_t
%llu
is not for Linux only; it's the format defined by the ISO C standard, starting with the 1999 edition. Unfortunately, Microsoft's C implementation doesn't support C99 or later. In other words, it's Windows, not Linux, that's the special case.