Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using macros in printf function in VS2013 vs VS2017

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.

like image 270
Arun Avatar asked Nov 29 '25 16:11

Arun


1 Answers

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

like image 186
Michael Burr Avatar answered Dec 02 '25 07:12

Michael Burr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!