简体   繁体   中英

Can I add new format specifier type to C printf in Linux platform

How can I add a new format specifier say %k which will print a specific user-defined structure in a specific format?

What i want to do:-

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

This printf should print the structure in user-defined fashion.

Yes, it's possible (but non-portable) using register_printf_function , see 12.13 Customizing printf from libc documentation for more details:

The GNU C Library lets you define your own custom conversion specifiers for printf template strings, to teach printf clever ways to print the important data structures of your program.

Here is an example how to create such custom format specifier for MAC address.

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