3

I have defined this macro in my source code

#define UINT_08X_FORMAT   "%08X"

I need to use the above in printf like this:

printf("Test - "UINT_08X_FORMAT"", 50);

It compiles and works fine in VS2013 where as in VS2017, it throws the following compile error.

invalid literal suffix 'UINT_08X_FORMAT'; literal operator or literal operator template 'operator ""UINT32_FORMAT' not found

How to use the macro in printf.

Note: I dont want to change the macro definition as it works fine with VS2013. I need a common solution which will work on both VS2013 and VS2017.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Arun
  • 2,247
  • 3
  • 28
  • 51

1 Answers1

4

C++11 added support for user defined literals (UDL), which are triggered by adding a suffix to some other literal (in this case a string literal). You can overcome it by adding spaces around your macro name to force the newer C++ compiler to treat it as a separate token instead of a UDL suffix:

printf("Test - " UINT_08X_FORMAT "", 50);

See this note from http://en.cppreference.com/w/cpp/language/user_literal:

Since the introduction of user-defined literals, the code that uses format macro constants for fixed-width integer types with no space after the preceding string literal became invalid: std::printf("%"PRId64"\n",INT64_MIN); has to be replaced by std::printf("%" PRId64"\n",INT64_MIN);

Due to maximal munch, user-defined integer and floating point literals ending in p, P, (since C++17) e and E, when followed by the operators + or -, must be separated from the operator with whitespace in the source

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • For clarification, change `printf("Test - "UINT_08X_FORMAT"", 50);` to `printf("Test - " UINT_08X_FORMAT "", 50);`. Just add spaces. – Sorry IwontTell Nov 28 '20 at 16:44