C Program to Get IP Address

This is a C Program to display the IP Address of the system.

Problem Description

This program displays the IP address of the system.

Problem Solution

1. Create a socket to define network interface IPv4.
2. Define the IPv4 address type.
3. Define the port name where network is attached.
4. Access the network interface information by passing address using ioctl.
5. Extract the IP address.

Program/Source Code

Here is source code of the C Program to display the IP Address of the system. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Get IP Address
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/ioctl.h>
  9. #include <netinet/in.h>
  10. #include <net/if.h>
  11. #include <unistd.h>
  12. #include <arpa/inet.h>
  13.  
  14. int main()
  15. {
  16.     int n;
  17.     struct ifreq ifr;
  18.     char array[] = "eth0";
  19.  
  20.     n = socket(AF_INET, SOCK_DGRAM, 0);
  21.     //Type of address to retrieve - IPv4 IP address
  22.     ifr.ifr_addr.sa_family = AF_INET;
  23.     //Copy the interface name in the ifreq structure
  24.     strncpy(ifr.ifr_name , array , IFNAMSIZ - 1);
  25.     ioctl(n, SIOCGIFADDR, &ifr);
  26.     close(n);
  27.     //display result
  28.     printf("IP Address is %s - %s\n" , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
  29.     return 0;
  30. }
Program Explanation

1. Create a socket to define network interface IPv4 using statement socket(AF_INET, SOCK_DGRAM, 0) and store it in the variable n.
2. Define the IPv4 address type by assigning AF_INET to (ifr.ifr_addr.sa_family).
3. Define the port name where network is attached using statement strncpy(ifr.ifr_name , array , IFNAMSIZ – 1), where array is initialized with string “etho”.
4. Call the ioctl function to access the network interface information by passing the address.
5. Close the variable n.
6. Use this (inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr)) to extract the IP address and print the same as output.

advertisement
advertisement
Runtime Test Cases
 
IP Address is eth0 - 192.168.225.135

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.