Posts tagged Wha is format specifier to view the ip address in c pro
Print IP address in dotted-decimal format without converting to string in C
Nov 10th
Few binary operations can be used to print a binary IP address in human-readable dotted-decimal format without using inet_ntoa() function that requires a string buffer. The following example shows how to do it.
[neo@techpulp ~]# cat pip.c
#include <stdio.h>
#define IP_QUAD(ip) (ip)>>24,((ip)&0x00ff0000)>>16,((ip)&0x0000ff00)>>8,((ip)&0x000000ff)
int main(int argc, char *argv[])
{
unsigned long my_ip = 0xa0653b20;
printf("My IP Address (0x%x) is %d.%d.%d.%d\n", my_ip, IP_QUAD(my_ip));
return 0;
}
[neo@techpulp ~]#
In the above program the macro IP_QUAD uses right-shift operator to extract each byte of the integer from MSB to LSB to expand it in to four arguments. This macro can be used pretty easily with “%u.%u.%u.%u” More >


Recent Comments