Sunday, 24 August 2014

Filled Under:

C Program to Display the IP Address of the System

Share


C Program to Display the IP Address of the System

C Program to Display the IP Address of the System: This C program display the IP address of a system.In fact it comes under the Socket programming.This C program runs successfully with Linux system.


C Program to Display the IP Address of the System


#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;
}


C Program to Display the IP Address of the System For windows 

#include<stdlib.h>
 
int main()
{
   system("C:\\Windows\\System32\\ipconfig");
 
   return 0;
}

Output

C Program to Display the IP Address of the System