Is it possible to write a new format specifier in C? For example, let's say, %g is a format specifier which prints the ip address in A.B.C.D format from the unsigned integer equivalent.
int ip_addr = Some integer
printf("ip address = %g", ip_addr);
output: prints ip address in A.B.C.D format
You would have to reimplement the printf function (more precisely its format parsing function). What you can do is wrap printf inside a function that supports ip addresses or do this:
#define FMT_IP "%d.%d.%d.%d"
#define FMT_IP_ARG(ip) getA(ip), getB(ip), getC(ip), getD(ip)
Where the getX functions or macros get the X part of the given integer representing an IP address.
Then:
printf("ip address = " FMT_IP "\n", FMT_IP_ARG(ip));
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