Can I add a new format specifier type in C printf on Linux?

How to add a specifier of a new format, say %k , which will print a specific user structure in a specific format?

What I want to do: -

 struct k { //members }s1; printf ("%k", s1); 

This printf must print the structure in a certain way.

+6
source share
1 answer

Yes, it is possible (but not portable) with register_printf_function , see 12.13 Configuring fff printing from libc documentation for more details:

The GNU C library allows you to define your own custom transform specifiers for printf pattern strings to teach printf smart ways to print important data structures of your program.

Here is an example of creating such a special format specifier for the MAC address.

+12
source

Source: https://habr.com/ru/post/972423/


All Articles