Dan
Dan

Reputation: 3341

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

I wrote a very simple test code of printf uint64_t:

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

int main()
{
  uint64_t ui64 = 90;
  printf("test uint64_t : %" PRIu64 "\n", ui64);
  return 0;
}

I use ubuntu 11.10 (64 bit) and gcc version 4.6.1 to compile it, but failed:

main.cpp: In function ‘int main()’:
main.cpp:9:30: error: expected ‘)’ before ‘PRIu64’
main.cpp:9:47: warning: spurious trailing ‘%’ in format [-Wformat]

Upvotes: 143

Views: 112451

Answers (3)

vitaut
vitaut

Reputation: 55745

Since you've included the C++ tag, you could use the {fmt} library and avoid the PRIu64 macro and other printf issues altogether:

#include <fmt/core.h>

int main() {
  uint64_t ui64 = 90;
  fmt::print("test uint64_t : {}\n", ui64);
}

The formatting facility based on this library is proposed for standardization in C++20: P0645.

Disclaimer: I'm the author of {fmt}.

Upvotes: 3

Anders Eliasson
Anders Eliasson

Reputation: 41

When compiling memcached under Centos 5.x i got the same problem.

The solution is to upgrade gcc and g++ to version 4.4 at least.

Make sure your CC/CXX is set (exported) to right binaries before compiling.

Upvotes: 4

Will
Will

Reputation: 75683

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

Upvotes: 178

Related Questions