This is a C Program to display the IP Address of the system.
This program displays the IP address of the system.
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.
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.
/*
* C Program to Get 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[] = "eth0";
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;
}
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.
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
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check C Books
- Apply for C Internship
- Check Computer Science Books