c++32bit-64bitgcc-warningc++98gcc-pedantic

C++98 pedantic errors when using fixed width integers


My company is moving from C slowly to C++98. C++98 is a superset of C so this shouldn't be a problem, but it is. Printing 64-bit fixed-width integers using printf does not work when used in combination with pedantic, warning flags, and specifying code for a 32-bit environment.

The numbers I need to print are of type uint64_t. And I understand usage of PRIX64 when using printf. However, as soon as the extra flags are added when compiling, errors ensue.

I've looked at the headers to see if there's anything weird, but it all looks good. I'm not sure why using this combination works in C but not C++. Of course the correct way to solve this would be to start using std::cout, but there's so much code to edit, it's not feasible to do it all at once.

Minimal example shown below (print.cpp):

#include <stdio.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

int main()
{
  uint64_t num = 0x0;
  printf("num is %" PRIX64"\n", num);

  return 0;
}

command used to compile it:

g++ print.cpp -o print  --std=c++98 -Wall -m32 --pedantic

Expected result: no errors, number is printed. Result:

warning: ISO C++ does not support the ‘ll’ gnu_printf length modifier

Solution

  • C++98 does not have uint64_t or PRIX64 (nor llx). Those were introduced to C++ in C++11 (side-note: They are not in C either until C99 standard).

    Those macros may happen to work by coincidence, since they are provided by the standard library which presumably has support for newer standard, and doesn't go out of its way to prevent older code from using the new features. But they are not guaranteed to work

    The compiler appears to not warn about macros presumably because the implementers were not exited about instrumenting the pre-processor to perform such diagnostics. But the compiler is friendly enough to diagnose the usage of a non-standard printf specifier (The llx specifier which PRIX64 macro expands to on your target system).