Get your current IP address by C

This article will help you to write a program to get IP address of your system. We can get our IP address in several way. You may have known another way to print IP address. Let’s see some C program to get IP address.

C program to get IP address

This is the simplest way to get your IP address if you are using windows operating system. Let’s go to the code.

// code here

Output of this get IP address program:


This will print your own IP address. Let’s see another C program which can only be used in Linux operating system to print IP address.


int main(){
  int num;
  struct ifreq ifr;
  char array[] = "eth0";

  num = socket(AF_INET, SOCK_DGRAM, 0);

  ifr.ifr_addr.sa_family = AF_INET;

  strncpy(ifr.ifr_name , array , IFNAMSIZ - 1);
  ioctl(num, SIOCGIFADDR, &ifr);

  printf("\nYour IP Address is %s - %s\n" , array , inet_ntoa(( (struct sockaddr_in *)&ifr.ifr_addr )->sin_addr) );
  return 0;

Previous pagePrint date and time

Next page:  Shutdown computer by C