30

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.

Virus721
  • 8,061
  • 12
  • 67
  • 123
  • Can you use std::cout? It should just work. – Neil Kirk Aug 07 '13 at 15:26
  • I can't. The function i use is a wrapper with variable argument list that uses printf and which i can't modify. – Virus721 Aug 07 '13 at 15:27
  • Read this also: [A funny thing with sprintf](http://stackoverflow.com/questions/17065203/a-funny-thing-with-sprintf/17065245#17065245) – Grijesh Chauhan Aug 07 '13 at 16:01
  • 6
    `%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. – Keith Thompson Aug 07 '13 at 16:48

6 Answers6

49

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.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
15

%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.

Wolf
  • 9,679
  • 7
  • 62
  • 108
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 3
    I wouldn't bother with this if i could choose what software i'm working with. – Virus721 Aug 07 '13 at 15:33
  • 4
    Unfortunately, Microsoft does not define a `ULONGLONG` to be an `unsigned long long`. Per [this page](http://msdn.microsoft.com/en-us/library/cc230393.aspx), 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. – Eric Postpischil Aug 07 '13 at 15:34
  • @Virus721 Your question says you need to use it in Windows, not saying Visual Studio. – Yu Hao Aug 07 '13 at 15:37
  • @YuHao: `ULONGLONG` and `__int64` are types in Visual Studio. – Eric Postpischil Aug 07 '13 at 15:40
  • 2
    @EricPostpischil didn't know that, sorry about that. But I think it's still worth pointing out `%llu` is not Linux only. It is Microsoft's fault not to follow C99. – Yu Hao Aug 07 '13 at 15:45
  • @YuHao It is not a fault. It is by design decision. Microsoft Visual C++ is not a C but C++ compiler which supports as much C standard(s) as C++98|03|11|14|17|... require – mloskot Apr 28 '17 at 10:55
  • @mloskot that is absolutely incorrect. Msvc supports e.g. naming variables `new` and `private` which goes far beyond what C++ requires – Antti Haapala -- Слава Україні Aug 15 '19 at 06:51
  • @AnttiHaapala "we don't say much about later C standards conformance, and this is intentional. MSVC includes a C89/C90 compliant compile (...) Support for later C standards is limited to the subset required by the C++ standard." https://github.com/MicrosoftDocs/cpp-docs/issues/407#issuecomment-417922008 – mloskot Aug 19 '19 at 21:13
  • %lld also worked for me although I think %llu is better – tulu May 11 '23 at 13:31
  • @mloskot [C++11 **1.1 Scope**, paragraph 2](https://port70.net/~nsz/c/c%2B%2B/c%2B%2B11_n3337.txt) (bolding mine): "C++ is a general purpose programming language based on the **C programming language as described in ISO/IEC 9899:1999** Programming languages" A **standard-conforming** C++11 implementation must therefore provide C99's `%ll` format modifier via `cstdio` and therefore the `[unsigned] long long` types. Any Microsoft implementation of C++11 that does not provide `[unsigned] long long` does not comply with the C++ standard. – Andrew Henle Jun 25 '23 at 13:49
8

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
    Visual Studio does not support `stdint.h` or `inttypes.h`. It does not support C 1999. There are [third-party implementations of them](http://code.google.com/p/msinttypes/). – Eric Postpischil Aug 07 '13 at 15:36
  • 3
    @EricPostpischil: Have guys at Microsoft completely lost their minds? :( –  Aug 07 '13 at 15:37
  • 1
    Thanks for your help, but %I64d does the job. – Virus721 Aug 07 '13 at 15:37
  • @EricPostpischil Fwiw, Visual Studio began `stdint.h` support with VS2010 (is use it regularly).But they still missed the bus on C99 (and from what I read, they not even bothering with it). Ugh. – WhozCraig Aug 07 '13 at 16:30
  • @WhozCraig: Does it support `inttypes.h` too? – Eric Postpischil Aug 07 '13 at 16:32
  • @EricPostpischil Not that i've seen, as evidenced by numerous wailings a gnashing of teeth from VS2010 articles across this great web of ours. – WhozCraig Aug 07 '13 at 16:35
  • 1
    @VladLazarenko: Microsoft is more interested in making money than in conforming to ISO standards. Apparently they've decided, so far, that C99 support is not worth the investment that would be required. I don't like it either (it slows general adoption of C99, to say nothing of C11), but I'm not in a position to judge whether their decision is correct in the context in which they've made it. They're under no obligation to support C99 -- or to support C at all, for that matter. (How many C compilers have *you* written?) – Keith Thompson Aug 07 '13 at 16:52
  • 1
    @KeithThompson: Totally agree with you on this one. Microsoft does not own us anything. I just wish that a company with such a resource and influence would do more good for the society and programmers in general. So far they just create inconsistencies :) –  Aug 07 '13 at 16:55
  • 1
    @VladLazarenko: They have said that they'll add at least some C99 support in the next release of Visual Studio. – Keith Thompson Aug 07 '13 at 16:58
2

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.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

Here is a work around for HEX output

printf("%08X%08X", static_cast<UINT32>((u64>>32)&0xFFFFFFFF), static_cast<UINT32>(u64)&0xFFFFFFFF));
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
0

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

starriet
  • 2,565
  • 22
  • 23