How to Printf Uint64_T? Fails With: "Spurious Trailing '%' in Format"

How to printf uint64_t? Fails with: spurious trailing ‘%’ in format

The ISO C99 standard specifies that these macros must only be defined if explicitly requested.

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

... now PRIu64 will work

How to print a int64_t type in C

For int64_t type:

#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);

for uint64_t type:

#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);

you can also use PRIx64 to print in hexadecimal.

cppreference.com has a full listing of available macros for all types including intptr_t (PRIxPTR). There are separate macros for scanf, like SCNd64.


A typical definition of PRIu16 would be "hu", so implicit string-constant concatenation happens at compile time.

For your code to be fully portable, you must use PRId32 and so on for printing int32_t, and "%d" or similar for printing int.

Format specifier to portably print std::uint64_t variable in C++11

PRIu64 is still what you will use in C++. Per [cinttypes.syn] PRIu64

[...]

#define PRIuN see below

[...]

The contents and meaning of the header <cinttypes> are the same as the C standard library header <inttypes.h> [...]

So it exists and has the same behavior that it does in the C ISO/IEC 9899:2011 standard.

Why doesn't PRIu64 work in this code?

One other possibility for this issue I just found in my own code is if another header already pulls in <inttypes.h> before you define __STDC_FORMAT_MACROS. For example:

Utils.h (Perhaps originally written for C, as it was in our case):

#include <inttypes.h>

// ... Function declarations

MyFile.cpp:

#include "Utils.h"

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

Since inttypes.h has already been included by Util.h, the compiler doesn't include it again, and doesn't see the declaration of __STDC_FORMAT_MACROS.

The solution is either to edit Utils.h to include #define __STDC_FORMAT_MACROS, or to make sure to define it before doing any includes in MyFile.cpp.

#define __STDC_FORMAT_MACROS
#include "Utils.h"
#include <inttypes.h>

The original setup actually compiled just fine on GCC 4.8 on Ubuntu, but failed with an old ltib GCC 4.3 toolchain for PowerPC, which made it all the more perplexing at first.

Want a solution for printf

#include <inttypes.h>
#include <stdio.h>

uint64_t t = 42;
printf("%" PRIu64 "\n", t);

Preprocessor division giving weird value

My bad:
I know I posted the code as:

printf("\n%X", (CurrentTimeInNanoSecs >> 32) );

but in reality I wrote it as:

printf("\n%X", (CurrentTimeInNanoSecs & 0xFFFFFFFF >> 32) );

So my upper 32 bits were always zero and I was misinterpreting the results :/

Thank you to the community though.

printf and %llu vs %lu on OS X

The macros are already defined for you in <cinttypes>. Try

printf("%"PRIu64, x);

Or, even better, use C++ features like

std::cout << x;

which will select the proper << operator for your variable type.



Related Topics



Leave a reply



Submit