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

c++ c
Dan picture Dan · Nov 15, 2011 · Viewed 102.9k times · Source

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]

Answer

Will picture Will · Nov 15, 2011

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