Is this possible to customize printf?

Sorry, but some answers are incorrect on Linux with Glibc

On Linux with a GNU Glibc, you can customize printf: you would call
register_printf_function to e.g. define the meaning of %Y in your printf format strings.

However, this behavior is Glibc specific, and might even become obsolete… I’m not sure I would recommend this approach!

If coding in C++, the C++ stream library has manipulators which you could extend, and you can also overload for your types the operator << etc.

added in february 2018

You could consider writing a GCC plugin helping that (and improving the typechecking of some extended printf). It won’t be easy (probably a few weeks or months of work), and it would be GCC version specific (not the same plugin code for GCC 7 and GCC 8). you might add some specific #pragma to inform your plugin about extra control string specifiers like your %Y and the type expected for them. Your plugin should change the handling of format attribute (perhaps in gcc/tree.c)

Leave a Comment