Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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.

like image 693
Hemanth Avatar asked Jul 17 '14 22:07

Hemanth


1 Answers

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.

like image 162
Grzegorz Szpetkowski Avatar answered Oct 21 '22 13:10

Grzegorz Szpetkowski