简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM