How to get the IP Address of local computer using C/C++?



In this article, we will see how to get the Host name and IP address of the local system in an easier way using C and C++ program. 

Getting IP Address of Local Computer 

For this, some standard networking functions are available in socket programming with header files like <winsock2.h> on Windows and <sys/socket.h>, <netdb.h>, <arpa/inet.h> on Linux. In this article we will mainly discuss three commonly used functions:

Sr.No Function & Description
1 gethostname()
It finds the standard host name for the local computer.
2 gethostbyname()
It finds the host information corresponding to a host name from host database
3 iten_ntoa()
It converts an IPv4 Internet network address into an ASCII string into dotted decimal format.

C Code to Get IP Address of Local Computer

Here is the following example code to get the Host name and IP address using the C language on a linux system.

In this program, we used header files <sys/socket.h>, <netdb.h>, <arpa/inet.h> to enable functions like gethostname() and gethostbyname() for retrieving the host name and IP address.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// This function returns host name for local computer
void check_host_name(int hostname) { 
   if (hostname == -1) {
      perror("gethostname");
      exit(1);
   }
}

// This function will the find host info from host name
void check_host_entry(struct hostent * hostentry) { 
   if (hostentry == NULL){
      perror("gethostbyname");
      exit(1);
   }
}

// This function will convert IP string to dotted decimal format
void IP_formatter(char *IPbuffer) { 
   if (NULL == IPbuffer) {
      perror("inet_ntoa");
      exit(1);
   }
}
int main() {
   char host[256];
   char *IP;
   struct hostent *host_entry;
   int hostname;
   
   // it will find the host name of current machine
   hostname = gethostname(host, sizeof(host));
   check_host_name(hostname);
   
   // it will find the host information
   host_entry = gethostbyname(host); 
   check_host_entry(host_entry);
   IP = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
   
   // It will convert into IP string
   printf("Current Host Name: %s\n", host);
   printf("Host IP: %s\n", IP);
}

Output

Current Host Name: 24abf9bb2a35
Host IP: 172.18.0.2

C++ Code to Get IP Address of Local Computer

Here is the following example code to get the Host name and IP address using the C++ language on a linux system.

In this program, we used header files <unistd.h>, <netdb.h> and <arpa/inet.h> to enable functions like gethostname() and gethostbyname() for retrieving the host name and IP address.

#include <iostream>
#include <unistd.h>        // this is for gethostname
#include <netdb.h>         // this is for gethostbyname
#include <arpa/inet.h>     // this is for inet_ntoa
#include <cstring>         

using namespace std;

int main() {
    char host[256];

    // to get local host name
    if (gethostname(host, sizeof(host)) == -1) {
        perror("gethostname failed");
        return 1;
    }

    cout << "Host Name: " << host << endl;

    // to get host information
    hostent* he = gethostbyname(host);
    if (he == nullptr) {
        cerr << "gethostbyname failed." << endl;
        return 1;
    }

    // to get and print the first IP address
    in_addr* addr = (in_addr*)he->h_addr_list[0];
    cout << "IP Address: " << inet_ntoa(*addr) << endl;

    return 0;
}

Output

Host Name: 24abf9bb2a35
IP Address: 172.18.0.2
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-11T18:01:43+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements