To get the IP Address of the system, you need interface name (i.e eth0 or wlan0 etc) and you have to call
"ioctl()" with flag "SIOCGIFADDR".
Here I am providing a c program which will print the ip address of the given interface.
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#define IF_NAME "wlan0" //Change this as per your interface name.
int main()
{
int sock, ret;
struct ifreq ifr;
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (-1 == sock) {
return 0;
}
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, IF_NAME, sizeof(ifr.ifr_name));
ret = ioctl(sock, SIOCGIFADDR, &ifr);
if (ret) {
goto error_sock;
}
struct sockaddr_in* ipaddr = (struct sockaddr_in*)&ifr.ifr_addr;
printf("IP address : %s\n", inet_ntoa(ipaddr->sin_addr));
error_sock:
close(sock);
return 0;
}
You can also use
getifaddrs() //see the man page for more info.