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 teachprintf
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With