C Programming Code Examples C > Miscellaneous Code Examples C Program to Display the IP Address of the System C Program to Display the IP Address of the System - Create a socket to define network interface IPv4. - Define the IPv4 address type. - Define the port name where network is attached. - Access the network interface information by passing address using ioctl. - Extract the IP address. #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <unistd.h> #include <arpa/inet.h> int main() { int n; struct ifreq ifr; char array[] = "xw23"; n = socket(AF_INET, SOCK_DGRAM, 0); //Type of address to retrieve - IPv4 IP address ifr.ifr_addr.sa_family = AF_INET; //Copy the interface name in the ifreq structure strncpy(ifr.ifr_name , array , IFNAMSIZ - 1); ioctl(n, SIOCGIFADDR, &ifr); close(n); //display result printf("IP Address is %s - %s\n" , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) ); return 0; } - Create a socket to define network interface IPv4 using statement socket(AF_INET, SOCK_DGRAM, 0) and store it in the variable n. - Define the IPv4 address type by assigning AF_INET to (ifr.ifr_addr.sa_family). - Define the port name where network is attached using statement strncpy(ifr.ifr_name , array , IFNAMSIZ - 1), where array is initialized with string "xw23". - Call the ioctl function to access the network interface information by passing the address. - Close the variable n. - Use this (inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr)) to extract the IP address and print the same as output.